Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: prevent empty version folders from being created #67

Merged
merged 1 commit into from
Nov 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
gablaxian marked this conversation as resolved.
Show resolved Hide resolved
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