Skip to content

Commit

Permalink
impl(internal/sidekick): extract shared testing data into variables
Browse files Browse the repository at this point in the history
Refactor tests to extract shared testing data into variables.
  • Loading branch information
julieqiu committed Nov 29, 2024
1 parent 1d06a57 commit cbee35d
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 37 deletions.
19 changes: 10 additions & 9 deletions generator/internal/sidekick/cmdline_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package sidekick

import (
"fmt"
"testing"

"github.com/google/go-cmp/cmp"
Expand All @@ -24,11 +25,11 @@ func TestParseArgs(t *testing.T) {
args := []string{
"-project-root", "../..",
"-specification-format", "openapi",
"-specification-source", "generator/testdata/openapi/secretmanager_openapi_v1.json",
"-service-config", "generator/testdata/googleapis/google/cloud/secretmanager/v1/secretmanager_v1.yaml",
"-source-option", "googleapis-root=generator/testdata/googleapis",
"-specification-source", specificationSource,
"-service-config", secretManagerServiceConfig,
"-source-option", fmt.Sprintf("googleapis-root=%s", googleapisRoot),
"-language", "rust",
"-output", "generator/testdata/test-only",
"-output", outputDir,
"-template-dir", "generator/templates",
"-codec-option", "copyright-year=2024",
"-codec-option", "package-name-override=secretmanager-golden-openapi",
Expand All @@ -45,14 +46,14 @@ func TestParseArgs(t *testing.T) {
Command: "generate",
ProjectRoot: "../..",
SpecificationFormat: "openapi",
SpecificationSource: "generator/testdata/openapi/secretmanager_openapi_v1.json",
ServiceConfig: "generator/testdata/googleapis/google/cloud/secretmanager/v1/secretmanager_v1.yaml",
SpecificationSource: specificationSource,
ServiceConfig: secretManagerServiceConfig,
Source: map[string]string{
"googleapis-root": "generator/testdata/googleapis",
"googleapis-root": googleapisRoot,
},
Language: "rust",
Output: "generator/testdata/test-only",
TemplateDir: "generator/templates",
Output: outputDir,
TemplateDir: templateDir,
Codec: map[string]string{
"copyright-year": "2024",
"package-name-override": "secretmanager-golden-openapi",
Expand Down
71 changes: 43 additions & 28 deletions generator/internal/sidekick/sidekick_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"os"
"os/exec"
"path"
"path/filepath"
"sort"
"strings"
"testing"
Expand All @@ -29,20 +30,29 @@ const (
// projectRoot is the root of the google-cloud-rust. The golden files for
// these tests depend on code in ../../auth and ../../src/gax.
projectRoot = "../../.."
templateDir = "generator/templates"
testdataDir = "generator/testdata"
)

var (
googleapisRoot = fmt.Sprintf("%s/googleapis", testdataDir)
outputDir = fmt.Sprintf("%s/test-only", testdataDir)
secretManagerServiceConfig = "googleapis/google/cloud/secretmanager/v1/secretmanager_v1.yaml"
specificationSource = fmt.Sprintf("%s/openapi/secretmanager_openapi_v1.json", testdataDir)
testdataImportPath = fmt.Sprintf("github.com/google-cloud-rust/%s", testdataDir)
)

func TestRustFromOpenAPI(t *testing.T) {
var outDir = fmt.Sprintf("%s/rust/openapi/golden", testdataDir)
cmdLine := &CommandLine{
Command: "generate",
ProjectRoot: projectRoot,
SpecificationFormat: "openapi",
SpecificationSource: "generator/testdata/openapi/secretmanager_openapi_v1.json",
ServiceConfig: "generator/testdata/googleapis/google/cloud/secretmanager/v1/secretmanager_v1.yaml",
SpecificationSource: specificationSource,
ServiceConfig: fmt.Sprintf("%s/%s", testdataDir, secretManagerServiceConfig),
Language: "rust",
Output: outDir,
TemplateDir: "generator/templates",
TemplateDir: templateDir,
Codec: map[string]string{
"copyright-year": "2024",
"package-name-override": "secretmanager-golden-openapi",
Expand All @@ -69,46 +79,51 @@ func TestRustFromProtobuf(t *testing.T) {

configs := []TestConfig{
{
Source: "generator/testdata/googleapis/google/type",
ServiceConfig: "generator/testdata/googleapis/google/type/type.yaml",
Source: "googleapis/google/type",
ServiceConfig: "googleapis/google/type/type.yaml",
Name: "type",
},
{
Source: "generator/testdata/googleapis/google/cloud/location",
ServiceConfig: "generator/testdata/googleapis/google/cloud/location/cloud.yaml",
Source: "googleapis/google/cloud/location",
ServiceConfig: "googleapis/google/cloud/location/cloud.yaml",
Name: "location",
},
{
Source: "generator/testdata/googleapis/google/iam/v1",
Source: "googleapis/google/iam/v1",
Name: "iam/v1",
ExtraOptions: map[string]string{
"package:gtype": "package=type-golden-gclient,path=generator/testdata/rust/gclient/golden/type,source=google.type",
"package:gtype": fmt.Sprintf("package=type-golden-gclient,path=%s/rust/gclient/golden/type,source=google.type", testdataDir),
},
},
{
Source: "generator/testdata/googleapis/google/cloud/secretmanager/v1",
ServiceConfig: "generator/testdata/googleapis/google/cloud/secretmanager/v1/secretmanager_v1.yaml",
Source: "googleapis/google/cloud/secretmanager/v1",
ServiceConfig: secretManagerServiceConfig,
Name: "secretmanager",
ExtraOptions: map[string]string{
"package:iam": "package=iam-v1-golden-gclient,path=generator/testdata/rust/gclient/golden/iam/v1,source=google.iam.v1",
"package:location": "package=location-golden-gclient,path=generator/testdata/rust/gclient/golden/location,source=google.cloud.location",
"package:iam": fmt.Sprintf("package=iam-v1-golden-gclient,path=%s/rust/gclient/golden/iam/v1,source=google.iam.v1", testdataDir),
"package:location": fmt.Sprintf("package=location-golden-gclient,path=%s/rust/gclient/golden/location,source=google.cloud.location", testdataDir),
},
},
}

for _, config := range configs {
if config.Source != "" {
config.Source = filepath.Join(testdataDir, config.Source)
}
if config.ServiceConfig != "" {
config.ServiceConfig = filepath.Join(testdataDir, config.ServiceConfig)
}
cmdLine := &CommandLine{
Command: "generate",
ProjectRoot: projectRoot,
SpecificationFormat: "protobuf",
SpecificationSource: config.Source,
Source: map[string]string{
"googleapis-root": "generator/testdata/googleapis",
"googleapis-root": googleapisRoot,
},
ServiceConfig: config.ServiceConfig,
Language: "rust",
Output: path.Join(outDir, config.Name),
TemplateDir: "generator/templates",
TemplateDir: templateDir,
Codec: map[string]string{
"copyright-year": "2024",
"package-name-override": strings.Replace(config.Name, "/", "-", -1) + "-golden-gclient",
Expand Down Expand Up @@ -165,12 +180,12 @@ func TestRustModuleFromProtobuf(t *testing.T) {
SpecificationFormat: "protobuf",
SpecificationSource: config.Source,
Source: map[string]string{
"googleapis-root": "generator/testdata/googleapis",
"googleapis-root": googleapisRoot,
},
ServiceConfig: config.ServiceConfig,
Language: "rust",
Output: path.Join("generator/testdata/rust/gclient/golden/module", config.Name),
TemplateDir: "generator/templates",
Output: path.Join(testdataDir, "rust/gclient/golden/module", config.Name),
TemplateDir: templateDir,
Codec: map[string]string{
"copyright-year": "2024",
"generate-module": "true",
Expand All @@ -195,23 +210,23 @@ func TestGoFromProtobuf(t *testing.T) {
}
configs := []TestConfig{
{
Source: "generator/testdata/googleapis/google/type",
Source: fmt.Sprintf("%s/google/type", googleapisRoot),
Name: "typez",
ExtraOptions: map[string]string{
"go-package-name": "typez",
},
},
{
Source: "generator/testdata/googleapis/google/iam/v1",
Source: fmt.Sprintf("%s/google/iam/v1", googleapisRoot),
Name: "iam/v1",
ExtraOptions: map[string]string{
"import-mapping:google.type": "github.com/google-cloud-rust/generator/testdata/go/gclient/golden/typez;typez",
"import-mapping:google.protobuf": "github.com/google-cloud-rust/generator/testdata/go/gclient/golden/wkt;wkt",
"import-mapping:google.type": fmt.Sprintf("%s/go/gclient/golden/typez;typez", testdataImportPath),
"import-mapping:google.protobuf": fmt.Sprintf("%s/go/gclient/golden/wkt;wkt", testdataImportPath),
"go-package-name": "iam",
},
ModReplace: map[string]string{
"github.com/google-cloud-rust/generator/testdata/go/gclient/golden/typez": "typez",
"github.com/google-cloud-rust/generator/testdata/go/gclient/golden/wkt": "wkt",
fmt.Sprintf("%s/go/gclient/golden/typez", testdataImportPath): "typez",
fmt.Sprintf("%s/go/gclient/golden/wkt", testdataImportPath): "wkt",
},
},
}
Expand All @@ -223,15 +238,15 @@ func TestGoFromProtobuf(t *testing.T) {
SpecificationFormat: "protobuf",
SpecificationSource: config.Source,
Source: map[string]string{
"googleapis-root": "generator/testdata/googleapis",
"googleapis-root": googleapisRoot,
},
ServiceConfig: "",
Language: "go",
Output: path.Join(outDir, config.Name),
TemplateDir: "generator/templates",
TemplateDir: templateDir,
Codec: map[string]string{
"copyright-year": "2024",
"package-name-override": "github.com/google-cloud-rust/generator/testdata/go/gclient/golden/" + config.Name,
"package-name-override": fmt.Sprintf("%s/go/gclient/golden/%s", testdataImportPath, config.Name),
},
}
for k, v := range config.ExtraOptions {
Expand Down

0 comments on commit cbee35d

Please sign in to comment.