-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Generate sample manifest file (#232)
- Loading branch information
Showing
4 changed files
with
183 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// Copyright 2019 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// https://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package gensample | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/googleapis/gapic-generator-go/internal/errors" | ||
) | ||
|
||
const ( | ||
latestSchemaVersion = "3" | ||
) | ||
|
||
// genManifest generates manifest file to be consumed by sample-tester. | ||
// See https://sample-tester.readthedocs.io/en/stable/defining-tests/manifest-reference.html | ||
// for the format of the manifest file. | ||
func (gen *generator) genManifest() error { | ||
// Do not generate sample manifest when there are no sample configs | ||
if len(gen.sampleConfig.Samples) == 0 { | ||
return nil | ||
} | ||
|
||
var b bytes.Buffer | ||
p := func(s string, args ...interface{}) { | ||
fmt.Fprintf(&b, s, args...) | ||
b.WriteByte('\n') | ||
} | ||
|
||
p("---") | ||
p("type: manifest/samples") | ||
p("schema_version: %s", latestSchemaVersion) | ||
p("go: &go") | ||
p(" environment: go") | ||
p(" invocation: go run {path}") | ||
p(" chdir: {@manifest_dir}/") | ||
p("samples:") | ||
|
||
for _, sp := range gen.sampleConfig.Samples { | ||
p("- <<: *go") | ||
p(" sample: %s", sp.ID) | ||
p(" path: %s.go", sp.ID) | ||
} | ||
|
||
api := gen.clientPkg.Name | ||
pos := strings.LastIndexByte(gen.clientPkg.Path, '/') | ||
if pos < 0 { | ||
return errors.E(nil, "expecting clientPkg path in 'url/to/client/apiversion/' format, got %q", gen.clientPkg.Path) | ||
} | ||
v := strings.Replace(gen.clientPkg.Path[pos+1:], "api", "", -1) | ||
gen.Outputs[fmt.Sprintf("%s.%s.go.manifest.yaml", api, v)] = b.Bytes() | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
// Copyright 2018 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// https://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package gensample | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/googleapis/gapic-generator-go/internal/errors" | ||
"github.com/googleapis/gapic-generator-go/internal/gensample/schema_v1p2" | ||
"github.com/googleapis/gapic-generator-go/internal/pbinfo" | ||
"github.com/googleapis/gapic-generator-go/internal/txtdiff" | ||
) | ||
|
||
func TestGenManifest(t *testing.T) { | ||
t.Parallel() | ||
|
||
g := &generator{ | ||
sampleConfig: schema_v1p2.SampleConfig{ | ||
Samples: []*schema_v1p2.Sample{ | ||
&schema_v1p2.Sample{ | ||
ID: "first_sample", | ||
}, | ||
&schema_v1p2.Sample{ | ||
ID: "second_sample", | ||
}, | ||
}, | ||
}, | ||
clientPkg: pbinfo.ImportSpec{Path: "path.to/client/apiv1", Name: "foo"}, | ||
Outputs: make(map[string][]byte), | ||
} | ||
|
||
if err := g.genManifest(); err != nil { | ||
t.Fatal(errors.E(err, "unexpected error.")) | ||
} | ||
|
||
// There is just one entry in g.Outputs, which is the manifest file | ||
for fn, contents := range g.Outputs { | ||
if fn != "foo.v1.go.manifest.yaml" { | ||
t.Fatal(errors.E(nil, "wrong manifest file name. Expected 'foo.v1.go.manifest.yaml', got %q", fn)) | ||
} | ||
compareManifest(t, contents, "testdata/manifest.want") | ||
} | ||
} | ||
|
||
func TestGenManifest_NoSamples(t *testing.T) { | ||
t.Parallel() | ||
|
||
g := &generator{ | ||
Outputs: make(map[string][]byte), | ||
} | ||
|
||
if err := g.genManifest(); err != nil { | ||
t.Fatal(errors.E(err, "unexpected error.")) | ||
} | ||
_, ok := g.Outputs["foo.v1.go.manifest.yaml"] | ||
if ok { | ||
t.Fatal(errors.E(nil, "manifest file should not be generated when there are no sample configs.")) | ||
} | ||
} | ||
|
||
func TestGenManifest_BadClientPkg_Fail(t *testing.T) { | ||
t.Parallel() | ||
|
||
g := &generator{ | ||
sampleConfig: schema_v1p2.SampleConfig{ | ||
Samples: []*schema_v1p2.Sample{ | ||
&schema_v1p2.Sample{ | ||
ID: "first_sample", | ||
}, | ||
&schema_v1p2.Sample{ | ||
ID: "second_sample", | ||
}, | ||
}, | ||
}, | ||
// Path is not in the format of `url/to/client/apiv1;name` | ||
clientPkg: pbinfo.ImportSpec{Path: "bad_path", Name: "foo"}, | ||
Outputs: make(map[string][]byte), | ||
} | ||
|
||
if err := g.genManifest(); err == nil { | ||
t.Fatal(errors.E(nil, "bad clientPkg, error expected")) | ||
} | ||
} | ||
|
||
func compareManifest(t *testing.T, got []byte, goldenPath string) { | ||
t.Helper() | ||
txtdiff.Diff(t, t.Name(), string(got), goldenPath) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
--- | ||
type: manifest/samples | ||
schema_version: 3 | ||
go: &go | ||
environment: go | ||
invocation: go run {path} | ||
chdir: {@manifest_dir}/ | ||
samples: | ||
- <<: *go | ||
sample: first_sample | ||
path: first_sample.go | ||
- <<: *go | ||
sample: second_sample | ||
path: second_sample.go |