damccorm commented on a change in pull request #16643: URL: https://github.com/apache/beam/pull/16643#discussion_r795286614
########## File path: sdks/go/pkg/beam/options/gcpopts/options_test.go ########## @@ -0,0 +1,79 @@ +// Licensed to the Apache Software Foundation (ASF) under one or more +// contributor license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright ownership. +// The ASF licenses this file to You 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 +// +// http://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 gcpopts + +import ( + "fmt" + "io/ioutil" + "os" + "testing" +) + +func TestGetProjectFromFlagOrEnvironmentWithNoProjectSet(t *testing.T) { + setupFakeCredentialFile(t, "") + *Project = "" + projectId := GetProjectFromFlagOrEnvironment(nil) + if projectId != "" { + t.Fatalf("%q returned as project id, should be \"\"", projectId) + } +} + +func TestGetProjectFromFlagOrEnvironmentWithProjectFlagSet(t *testing.T) { + setupFakeCredentialFile(t, "") + *Project = "test" + projectId := GetProjectFromFlagOrEnvironment(nil) + if projectId != "test" { + t.Fatalf("%q returned as project id, should be \"test\"", projectId) + } +} + +func TestGetProjectFromFlagOrEnvironmentWithNoProjectFlagSetAndFallbackSet(t *testing.T) { + setupFakeCredentialFile(t, "fallback") + *Project = "" + projectId := GetProjectFromFlagOrEnvironment(nil) + if projectId != "fallback" { + t.Fatalf("%q returned as project id, should be \"fallback\"", projectId) + } +} + +func TestGetProjectFromFlagOrEnvironmentWithProjectFlagSetAndFallbackSet(t *testing.T) { + setupFakeCredentialFile(t, "fallback") + *Project = "test" + projectId := GetProjectFromFlagOrEnvironment(nil) + if projectId == "fallback" { + t.Fatalf("fallback returned as project id, should have used the flag setting of \"test\"") + } else if projectId != "test" { + t.Fatalf("%q returned as project id, should be \"test\"", projectId) + } +} + +// Set up fake credential file to read project from with the passed in projectId. +func setupFakeCredentialFile(t *testing.T, projectId string) { Review comment: Thanks - I was actually confused on how Go disambiguated between helper functions (I'd inferred incorrectly it was based on the args) ########## File path: sdks/go/pkg/beam/options/gcpopts/options.go ########## @@ -37,9 +39,22 @@ var ( // GetProject returns the project, if non empty and exits otherwise. // Convenience function. func GetProject(ctx context.Context) string { - if *Project == "" { + project := GetProjectFromFlagOrEnvironment(ctx) Review comment: Good call - I'd initially missed this and then caught that this function needed updated as well as I was self code-reviewing, clearly missed the test though! -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
