Skip to content

Commit

Permalink
Merge pull request #67 from snyk/fix/compile-redundant-folders
Browse files Browse the repository at this point in the history
fix: prevent empty version folders from being created
  • Loading branch information
gablaxian authored Nov 11, 2021
2 parents 9bc2f48 + a3b2cae commit df6018b
Show file tree
Hide file tree
Showing 29 changed files with 41 additions and 5,972 deletions.
13 changes: 6 additions & 7 deletions cmd/compiler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ func TestCompile(t *testing.T) {
version string
paths []string
}{{
version: "2021-06-01",
version: "2021-06-01~experimental",
paths: []string{"/examples/hello-world/{id}"},
}, {
version: "2021-06-07",
version: "2021-06-07~experimental",
paths: []string{"/examples/hello-world/{id}"},
}, {
version: "2021-06-13~beta",
Expand Down Expand Up @@ -55,22 +55,21 @@ func TestCompileInclude(t *testing.T) {
tests := []struct {
version string
}{{
version: "2021-06-01",
version: "2021-06-01~experimental",
}, {
version: "2021-06-07",
version: "2021-06-07~experimental",
}, {
version: "2021-06-13~beta",
}, {
version: "2021-06-04~experimental",
}}
for _, test := range tests {
v, err := vervet.ParseVersion(test.version)
c.Assert(err, qt.IsNil)
// Load just-compiled OpenAPI YAML file
doc, err := vervet.NewDocumentFile(dstDir + "/" + v.DateString() + "/spec.yaml")
doc, err := vervet.NewDocumentFile(dstDir + "/" + test.version + "/spec.yaml")
c.Assert(err, qt.IsNil)

expected, err := ioutil.ReadFile(testdata.Path("output/" + v.DateString() + "/spec.json"))
expected, err := ioutil.ReadFile(testdata.Path("output/" + test.version + "/spec.json"))
c.Assert(err, qt.IsNil)

// Servers will differ between the fixture output and the above, since
Expand Down
4 changes: 2 additions & 2 deletions cmd/version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ func TestVersionList(t *testing.T) {
+----------+-------------+-------------------------+----------------------------+--------+------------------+
| API | RESOURCE | VERSION | PATH | METHOD | OPERATION |
+----------+-------------+-------------------------+----------------------------+--------+------------------+
| testdata | hello-world | 2021-06-01 | /examples/hello-world/{id} | GET | helloWorldGetOne |
| testdata | hello-world | 2021-06-07 | /examples/hello-world/{id} | GET | helloWorldGetOne |
| testdata | hello-world | 2021-06-01~experimental | /examples/hello-world/{id} | GET | helloWorldGetOne |
| testdata | hello-world | 2021-06-07~experimental | /examples/hello-world/{id} | GET | helloWorldGetOne |
| testdata | hello-world | 2021-06-13~beta | /examples/hello-world | POST | helloWorldCreate |
| testdata | hello-world | 2021-06-13~beta | /examples/hello-world/{id} | GET | helloWorldGetOne |
| testdata | projects | 2021-06-04~experimental | /orgs/{orgId}/projects | GET | getOrgsProjects |
Expand Down
16 changes: 11 additions & 5 deletions internal/compiler/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,18 +286,24 @@ func (c *Compiler) Build(ctx context.Context, apiName string) error {
if err != nil {
return buildErr(err)
}
versionDir := api.output.path + "/" + version.String()
err = os.MkdirAll(versionDir, 0755)
if err != nil {
return buildErr(err)
}

spec, err := specVersions.At(version.String())
if err == vervet.ErrNoMatchingVersion {
continue
} else if err != nil {
return buildErr(err)
}

// Create the directories, but only if a spec file exists for it.
versionDir := api.output.path + "/" + version.String()

if spec != nil {
err = os.MkdirAll(versionDir, 0755)
if err != nil {
return buildErr(err)
}
}

// Merge all overlays
for _, doc := range api.overlayIncludes {
vervet.Merge(spec, doc.T, true)
Expand Down
9 changes: 9 additions & 0 deletions internal/compiler/compiler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,15 @@ func TestCompilerSmoke(t *testing.T) {
err = compiler.BuildAll(ctx)
c.Assert(err, qt.IsNil)

// Verify created files/folders are as expected
// Look for existence of /2021-06-01~experimental
_, err = os.Stat(outputPath + "/2021-06-01~experimental")
c.Assert(err, qt.IsNil)

// Look for absence of /2021-06-01 folder (ga)
_, err = os.Stat(outputPath + "/2021-06-01")
c.Assert(os.IsNotExist(err), qt.IsTrue)

// Build output was cleaned up
_, err = ioutil.ReadFile(outputPath + "/goof")
c.Assert(err, qt.ErrorMatches, ".*/goof: no such file or directory")
Expand Down
9 changes: 3 additions & 6 deletions resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ func TestResource(t *testing.T) {
c.Assert(err, qt.IsNil)
c.Assert(eps.Versions(), qt.DeepEquals, []Version{{
Date: time.Date(2021, time.June, 1, 0, 0, 0, 0, time.UTC),
Stability: StabilityGA,
Stability: StabilityExperimental,
}, {
Date: time.Date(2021, time.June, 7, 0, 0, 0, 0, time.UTC),
Stability: StabilityGA,
Stability: StabilityExperimental,
}, {
Date: time.Date(2021, time.June, 13, 0, 0, 0, 0, time.UTC),
Stability: StabilityBeta,
Expand All @@ -40,17 +40,14 @@ func TestVersionRangesHelloWorld(t *testing.T) {
tests := []struct {
query, match string
}{{
query: "2021-07-01",
match: "2021-06-07",
}, {
query: "2021-07-01~experimental",
match: "2021-06-13~beta",
}, {
query: "2021-07-01~beta",
match: "2021-06-13~beta",
}, {
query: "2021-06-08~experimental",
match: "2021-06-07",
match: "2021-06-07~experimental",
}}
for _, t := range tests {
e, err := eps.At(t.query)
Expand Down
10 changes: 2 additions & 8 deletions spec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ func TestSpecs(t *testing.T) {
versions := specs.Versions()
c.Assert(versions, qt.HasLen, 4)
c.Assert(versions, qt.ContentEquals, []Version{
MustParseVersion("2021-06-01"),
MustParseVersion("2021-06-01~experimental"),
MustParseVersion("2021-06-04~experimental"),
MustParseVersion("2021-06-07"),
MustParseVersion("2021-06-07~experimental"),
MustParseVersion("2021-06-13~beta"),
})

Expand Down Expand Up @@ -62,12 +62,6 @@ func TestSpecs(t *testing.T) {
version: "2021-06-13~beta",
path: "/examples/hello-world/{id}",
}},
}, {
query: "2021-07-01",
hasVersions: []expectResourceVersion{{
version: "2021-06-07",
path: "/examples/hello-world/{id}",
}},
}}
for i, t := range tests {
c.Logf("test#%d: %#v", i, t)
Expand Down
Loading

0 comments on commit df6018b

Please sign in to comment.