From 117db2ad8b20e8a235bd7d35da6d9fb6f1878645 Mon Sep 17 00:00:00 2001 From: Aidan Feldman Date: Fri, 26 Aug 2016 20:25:57 -0400 Subject: [PATCH] share fixture helper functions between tests --- control/summary_table_test.go | 33 +++++---------------------- fixtures/fixtures.go | 43 +++++++++++++++++++++++++++++++++++ ssp/document_test.go | 14 +++--------- templater/templater_test.go | 34 ++++----------------------- 4 files changed, 57 insertions(+), 67 deletions(-) create mode 100644 fixtures/fixtures.go diff --git a/control/summary_table_test.go b/control/summary_table_test.go index 9af7624..41ff9c1 100644 --- a/control/summary_table_test.go +++ b/control/summary_table_test.go @@ -2,14 +2,12 @@ package control_test import ( "bytes" - "os" - "path/filepath" "text/template" "github.com/jbowtie/gokogiri/xml" . "github.com/opencontrol/fedramp-templater/control" "github.com/opencontrol/fedramp-templater/docx/helper" - "github.com/opencontrol/fedramp-templater/opencontrols" + "github.com/opencontrol/fedramp-templater/fixtures" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" @@ -21,7 +19,7 @@ type tableData struct { } func docFixture(control string) *xml.XmlDocument { - path := filepath.Join("..", "fixtures", "simplified_table.xml") + path := fixtures.FixturePath("simplified_table.xml") tpl, err := template.ParseFiles(path) Expect(err).ToNot(HaveOccurred()) @@ -42,32 +40,12 @@ func getTable(control string) xml.Node { return tables[0] } -func openControlFixturePath() string { - path := filepath.Join("..", "fixtures", "opencontrols") - path, err := filepath.Abs(path) - Expect(err).NotTo(HaveOccurred()) - _, err = os.Stat(path) - Expect(err).NotTo(HaveOccurred()) - - return path -} - -func openControlFixture() opencontrols.Data { - path := openControlFixturePath() - data, errors := opencontrols.LoadFrom(path) - for _, err := range errors { - Expect(err).NotTo(HaveOccurred()) - } - - return data -} - var _ = Describe("SummaryTable", func() { Describe("Fill", func() { It("fills in the Responsible Role for controls", func() { table := getTable("AC-2") ct := SummaryTable{Root: table} - openControlData := openControlFixture() + openControlData := fixtures.LoadOpenControlFixture() ct.Fill(openControlData) @@ -77,13 +55,14 @@ var _ = Describe("SummaryTable", func() { It("fills in the Responsible Role for control enhancements", func() { table := getTable("AC-2 (1)") ct := SummaryTable{Root: table} - openControlData := openControlFixture() + openControlData := fixtures.LoadOpenControlFixture() ct.Fill(openControlData) Expect(table.Content()).To(ContainSubstring(`Responsible Role: Amazon Elastic Compute Cloud: AWS Staff`)) }) }) + Describe("Diff", func() { It("detects no diff when the value of responsible role is empty", func() { doc := docFixture("AC-2") @@ -91,7 +70,7 @@ var _ = Describe("SummaryTable", func() { table := tables[0] ct := SummaryTable{Root: table} - openControlData := openControlFixture() + openControlData := fixtures.LoadOpenControlFixture() diff, err := ct.Diff(openControlData) Expect(diff).To(Equal([]reporter.Reporter{})) diff --git a/fixtures/fixtures.go b/fixtures/fixtures.go new file mode 100644 index 0000000..4087519 --- /dev/null +++ b/fixtures/fixtures.go @@ -0,0 +1,43 @@ +package fixtures + +import ( + "os" + "path/filepath" + + . "github.com/onsi/gomega" + "github.com/opencontrol/fedramp-templater/opencontrols" + "github.com/opencontrol/fedramp-templater/ssp" +) + +func FixturePath(name string) string { + path := filepath.Join("..", "fixtures", name) + path, err := filepath.Abs(path) + Expect(err).NotTo(HaveOccurred()) + // ensure the file/directory exists + _, err = os.Stat(path) + Expect(err).NotTo(HaveOccurred()) + + return path +} + +func OpenControlFixturePath() string { + return FixturePath("opencontrols") +} + +func LoadSSP(name string) *ssp.Document { + sspPath := FixturePath(name) + doc, err := ssp.Load(sspPath) + Expect(err).NotTo(HaveOccurred()) + + return doc +} + +func LoadOpenControlFixture() opencontrols.Data { + openControlDir := OpenControlFixturePath() + openControlData, errors := opencontrols.LoadFrom(openControlDir) + for _, err := range errors { + Expect(err).NotTo(HaveOccurred()) + } + + return openControlData +} diff --git a/ssp/document_test.go b/ssp/document_test.go index 88c5c4b..6d970d4 100644 --- a/ssp/document_test.go +++ b/ssp/document_test.go @@ -1,25 +1,17 @@ package ssp_test import ( - "path/filepath" - + "github.com/opencontrol/fedramp-templater/fixtures" . "github.com/opencontrol/fedramp-templater/ssp" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" ) -func loadSSP(name string) *Document { - path := filepath.Join("..", "fixtures", name) - doc, err := Load(path) - Expect(err).NotTo(HaveOccurred()) - return doc -} - var _ = Describe("SSP", func() { Describe("Load", func() { It("gets the content from the doc", func() { - doc := loadSSP("FedRAMP_ac-2-1_v2.1.docx") + doc := fixtures.LoadSSP("FedRAMP_ac-2-1_v2.1.docx") defer doc.Close() Expect(doc.Content()).To(ContainSubstring("Control Enhancement")) @@ -33,7 +25,7 @@ var _ = Describe("SSP", func() { Describe("SummaryTables", func() { It("returns the tables", func() { - doc := loadSSP("FedRAMP_ac-2_v2.1.docx") + doc := fixtures.LoadSSP("FedRAMP_ac-2_v2.1.docx") defer doc.Close() tables, err := doc.SummaryTables() diff --git a/templater/templater_test.go b/templater/templater_test.go index e3a58e4..e24393c 100644 --- a/templater/templater_test.go +++ b/templater/templater_test.go @@ -2,10 +2,8 @@ package templater_test import ( "bytes" - "path/filepath" - "github.com/opencontrol/fedramp-templater/opencontrols" - "github.com/opencontrol/fedramp-templater/ssp" + "github.com/opencontrol/fedramp-templater/fixtures" . "github.com/opencontrol/fedramp-templater/templater" . "github.com/onsi/ginkgo" @@ -13,26 +11,6 @@ import ( "github.com/opencontrol/fedramp-templater/reporter" ) -func loadSSP(name string) *ssp.Document { - sspPath := filepath.Join("..", "fixtures", name) - doc, err := ssp.Load(sspPath) - Expect(err).NotTo(HaveOccurred()) - - return doc -} - -func loadOpenControlFixture() opencontrols.Data { - openControlDir := filepath.Join("..", "fixtures", "opencontrols") - openControlDir, err := filepath.Abs(openControlDir) - Expect(err).NotTo(HaveOccurred()) - openControlData, errors := opencontrols.LoadFrom(openControlDir) - for _, err := range errors { - Expect(err).NotTo(HaveOccurred()) - } - - return openControlData -} - func extractDiffReport(reporters []reporter.Reporter) string { report := &bytes.Buffer{} for _, rept := range reporters { @@ -44,9 +22,9 @@ func extractDiffReport(reporters []reporter.Reporter) string { var _ = Describe("Templater", func() { Describe("TemplatizeSSP", func() { It("fills in the Responsible Role fields", func() { - doc := loadSSP("FedRAMP_ac-2-1_v2.1.docx") + doc := fixtures.LoadSSP("FedRAMP_ac-2-1_v2.1.docx") defer doc.Close() - openControlData := loadOpenControlFixture() + openControlData := fixtures.LoadOpenControlFixture() err := TemplatizeSSP(doc, openControlData) @@ -62,14 +40,12 @@ var _ = Describe("Templater", func() { By("Loading the SSP with the Responsible Role being 'OpenControl Role Placeholder' " + "for Control 'AC-2 (1)'") - sspPath := filepath.Join("..", "fixtures", "FedRAMP_ac-2-1_v2.1.docx") - s, err := ssp.Load(sspPath) - Expect(err).NotTo(HaveOccurred()) + s := fixtures.LoadSSP("FedRAMP_ac-2-1_v2.1.docx") defer s.Close() By("Loading the data from the opencontrol workspace with the Responsible Role being " + "'Amazon Elastic Compute Cloud: AWS Staff' for Control 'AC-2 (1)'") - openControlData := loadOpenControlFixture() + openControlData := fixtures.LoadOpenControlFixture() By("Calling 'diff' on the SSP") diffInfo, err := DiffSSP(s, openControlData)