Skip to content

Commit

Permalink
Create test_masonry.go for Masonry func
Browse files Browse the repository at this point in the history
  • Loading branch information
redhatrises committed Jun 7, 2018
1 parent 891c33b commit ce31a59
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 65 deletions.
22 changes: 2 additions & 20 deletions cmd/compliance-masonry/compliance-masonry_test.go
Original file line number Diff line number Diff line change
@@ -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 = `
Expand All @@ -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
}
25 changes: 4 additions & 21 deletions pkg/cli/diff/diff_test.go
Original file line number Diff line number Diff line change
@@ -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
}
2 changes: 1 addition & 1 deletion pkg/cli/docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
29 changes: 6 additions & 23 deletions pkg/cli/docs/docs_test.go
Original file line number Diff line number Diff line change
@@ -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"
)

Expand Down Expand Up @@ -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"))
})
})
Expand All @@ -104,22 +101,22 @@ 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"))
})
})
})

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"),
Expand All @@ -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"),
Expand All @@ -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
}
1 change: 1 addition & 0 deletions pkg/cmd/masonry/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
26 changes: 26 additions & 0 deletions pkg/tests/test_masonry.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit ce31a59

Please sign in to comment.