diff --git a/cmd/compliance-masonry/compliance-masonry_test.go b/cmd/compliance-masonry/compliance-masonry_test.go index 0302639b..81f0fad2 100644 --- a/cmd/compliance-masonry/compliance-masonry_test.go +++ b/cmd/compliance-masonry/compliance-masonry_test.go @@ -1,13 +1,9 @@ package main_test import ( - "bufio" - "os/exec" - "strings" - . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" - . "github.com/onsi/gomega/gexec" + "github.com/opencontrol/compliance-masonry/pkg/tests" ) var usage = ` @@ -30,22 +26,8 @@ Flags: var _ = Describe("Masonry CLI", func() { Describe("When the CLI is run with no commands", func() { It("should list the available commands", func() { - output := Masonry() + output := testMasonry.Masonry() Eventually(output.Out.Contents).Should(ContainSubstring(usage)) }) }) }) - -func Masonry(args ...string) *Session { - path, err := Build("github.com/opencontrol/compliance-masonry/cmd/compliance-masonry") - Expect(err).NotTo(HaveOccurred()) - cmd := exec.Command(path, args...) - stdin, err := cmd.StdinPipe() - Expect(err).ToNot(HaveOccurred()) - buffer := bufio.NewWriter(stdin) - _, _ = buffer.WriteString(strings.Join(args, " ")) - _ = buffer.Flush() - session, err := Start(cmd, GinkgoWriter, GinkgoWriter) - Expect(err).NotTo(HaveOccurred()) - return session -} diff --git a/pkg/cli/diff/diff_test.go b/pkg/cli/diff/diff_test.go index 13bb3945..4bd772ad 100644 --- a/pkg/cli/diff/diff_test.go +++ b/pkg/cli/diff/diff_test.go @@ -1,50 +1,33 @@ package diff_test import ( - "bufio" - "os/exec" "path/filepath" - "strings" "time" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" - . "github.com/onsi/gomega/gexec" + "github.com/opencontrol/compliance-masonry/pkg/tests" ) var _ = Describe("Diff Commands", func() { Describe("When the diff command is run", func() { It("should let the user know that they have not described a certification and show how to use the command", func() { - output := Masonry("diff") + output := testMasonry.Masonry("diff") Eventually(output.Err.Contents).Should(ContainSubstring("certification type not specified\n")) }) }) Describe("When the CLI is run with the `diff` command without opencontrols dir", func() { It("should let the user know that there is no opencontrols/certifications directory", func() { - output := Masonry("diff", "LATO") + output := testMasonry.Masonry("diff", "LATO") Eventually(output.Err.Contents).Should(ContainSubstring("Error: `" + filepath.Join("opencontrols", "certifications") + "` directory does exist\n")) }) }) Describe("When the CLI is run with the `diff` command with a certification", func() { It("should print the number of missing controls", func() { - output := Masonry( + output := testMasonry.Masonry( "diff", "LATO", "-o", filepath.Join("..", "..", "..", "test", "fixtures", "opencontrol_fixtures")).Wait(1 * time.Second) Eventually(output.Out.Contents).Should(ContainSubstring("Number of missing controls:")) }) }) }) - -func Masonry(args ...string) *Session { - path, err := Build("github.com/opencontrol/compliance-masonry/cmd/compliance-masonry") - Expect(err).NotTo(HaveOccurred()) - cmd := exec.Command(path, args...) - stdin, err := cmd.StdinPipe() - Expect(err).ToNot(HaveOccurred()) - buffer := bufio.NewWriter(stdin) - _, _ = buffer.WriteString(strings.Join(args, " ")) - _ = buffer.Flush() - session, err := Start(cmd, GinkgoWriter, GinkgoWriter) - Expect(err).NotTo(HaveOccurred()) - return session -} diff --git a/pkg/cli/docs/docs.go b/pkg/cli/docs/docs.go index 54fb09e4..8c9ba4b5 100644 --- a/pkg/cli/docs/docs.go +++ b/pkg/cli/docs/docs.go @@ -22,7 +22,7 @@ func NewCmdDocs(out io.Writer) *cobra.Command { return cmd } -// NewCmDocsGitBook creates the compliance documentation in Gitbook format. +// NewCmdDocsGitBook creates the compliance documentation in Gitbook format. func NewCmdDocsGitBook(out io.Writer) *cobra.Command { cmd := &cobra.Command{ Use: "gitbook", diff --git a/pkg/cli/docs/docs_test.go b/pkg/cli/docs/docs_test.go index e07734ed..0baba572 100644 --- a/pkg/cli/docs/docs_test.go +++ b/pkg/cli/docs/docs_test.go @@ -1,23 +1,20 @@ package docs_test import ( - "bufio" "errors" "fmt" "io/ioutil" "os" - "os/exec" "path/filepath" - "strings" "time" . "github.com/opencontrol/compliance-masonry/pkg/cli/docs" "github.com/opencontrol/compliance-masonry/pkg/cli/docs/gitbook" + "github.com/opencontrol/compliance-masonry/pkg/tests" . "github.com/onsi/ginkgo" . "github.com/onsi/ginkgo/extensions/table" . "github.com/onsi/gomega" - . "github.com/onsi/gomega/gexec" "github.com/stretchr/testify/assert" ) @@ -88,7 +85,7 @@ var _ = Describe("Doc Tests", func() { Describe("Base Docs Commands", func() { Describe("When the CLI is run with the docs command", func() { It("should list the available doc commands", func() { - output := Masonry("docs", "") + output := testMasonry.Masonry("docs", "") Eventually(output.Out.Contents).Should(ContainSubstring("Create compliance documentation in Gitbook format")) }) }) @@ -104,14 +101,14 @@ var _ = Describe("Doc Tests", func() { Describe("Gitbook Commands", func() { Describe("When the CLI is run with the `docs gitbook` command", func() { It("should let the user know that they have not described a certification and show how to use the command", func() { - output := Masonry("docs", "gitbook") + output := testMasonry.Masonry("docs", "gitbook") Eventually(output.Err.Contents).Should(ContainSubstring("certification type not specified\n")) }) }) Describe("When the CLI is run with the `docs gitbook` command without opencontrols dir", func() { It("should let the user know that there is no opencontrols/certifications directory", func() { - output := Masonry("docs", "gitbook", "LATO") + output := testMasonry.Masonry("docs", "gitbook", "LATO") Eventually(output.Err.Contents).Should(ContainSubstring("Error: `" + filepath.Join("opencontrols", "certifications") + "` directory does exist\n")) }) }) @@ -119,7 +116,7 @@ var _ = Describe("Doc Tests", func() { Describe("When the CLI is run with the `docs gitbook` command with a certification and no markdown", func() { It("should create the documentation but warn users that there is no markdown dir", func() { - output := Masonry( + output := testMasonry.Masonry( "docs", "gitbook", "LATO", "-e", exportTempDir, "-o", filepath.Join("..", "..", "..", "test", "fixtures", "opencontrol_fixtures"), @@ -132,7 +129,7 @@ var _ = Describe("Doc Tests", func() { Describe("When the CLI is run with the `docs gitbook` command with a certification", func() { It("should create the documentation without warning the user", func() { exportTempDir, _ := ioutil.TempDir("", "exports") - output := Masonry( + output := testMasonry.Masonry( "docs", "gitbook", "LATO", "-e", exportTempDir, "-o", filepath.Join("..", "..", "..", "test", "fixtures", "opencontrol_fixtures_with_markdown"), @@ -146,17 +143,3 @@ var _ = Describe("Doc Tests", func() { }) }) }) - -func Masonry(args ...string) *Session { - path, err := Build("github.com/opencontrol/compliance-masonry/cmd/compliance-masonry") - Expect(err).NotTo(HaveOccurred()) - cmd := exec.Command(path, args...) - stdin, err := cmd.StdinPipe() - Expect(err).ToNot(HaveOccurred()) - buffer := bufio.NewWriter(stdin) - _, _ = buffer.WriteString(strings.Join(args, " ")) - _ = buffer.Flush() - session, err := Start(cmd, GinkgoWriter, GinkgoWriter) - Expect(err).NotTo(HaveOccurred()) - return session -} diff --git a/pkg/cmd/masonry/cmd.go b/pkg/cmd/masonry/cmd.go index bbdb945b..2bcb7836 100644 --- a/pkg/cmd/masonry/cmd.go +++ b/pkg/cmd/masonry/cmd.go @@ -17,6 +17,7 @@ import ( // Verbose boolean for turning on/off verbosity var Verbose bool + // Version for cli variable holding program version var Version bool diff --git a/pkg/tests/test_masonry.go b/pkg/tests/test_masonry.go new file mode 100644 index 00000000..4088ded0 --- /dev/null +++ b/pkg/tests/test_masonry.go @@ -0,0 +1,26 @@ +package testMasonry + +import ( + "bufio" + "os/exec" + "strings" + + "github.com/onsi/ginkgo" + "github.com/onsi/gomega" + "github.com/onsi/gomega/gexec" +) + +// Masonry is used to launch tests on the CLI +func Masonry(args ...string) *Session { + path, err := Build("github.com/opencontrol/compliance-masonry/cmd/compliance-masonry") + Expect(err).NotTo(HaveOccurred()) + cmd := exec.Command(path, args...) + stdin, err := cmd.StdinPipe() + Expect(err).ToNot(HaveOccurred()) + buffer := bufio.NewWriter(stdin) + _, _ = buffer.WriteString(strings.Join(args, " ")) + _ = buffer.Flush() + session, err := Start(cmd, GinkgoWriter, GinkgoWriter) + Expect(err).NotTo(HaveOccurred()) + return session +}