Skip to content

Commit

Permalink
Generate sample manifest file (#232)
Browse files Browse the repository at this point in the history
  • Loading branch information
yihanzhen authored Oct 15, 2019
1 parent 2d7a6a9 commit 386d4fa
Show file tree
Hide file tree
Showing 4 changed files with 183 additions and 1 deletion.
67 changes: 67 additions & 0 deletions internal/gensample/genmanifest.go
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
}
100 changes: 100 additions & 0 deletions internal/gensample/genmanifest_test.go
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)
}
3 changes: 2 additions & 1 deletion internal/gensample/gensample.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,8 @@ func (gen *generator) GenMethodSamples() error {
fname := samp.ID + ".go"
gen.Outputs[fname] = content
}
return nil

return gen.genManifest()
}

type generator struct {
Expand Down
14 changes: 14 additions & 0 deletions internal/gensample/testdata/manifest.want
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

0 comments on commit 386d4fa

Please sign in to comment.