lostluck commented on a change in pull request #16643:
URL: https://github.com/apache/beam/pull/16643#discussion_r795933968



##########
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:
       Ultimately, Go tries to allow you to use as much of Go as possible when 
writing your tests and such. This one is a limited case technically. What the 
"t.Helper()" call does is cause a failure to be reported at the helper's Call 
Site, rather than at which line in the helper failed. Eg. If one has customized 
a check and is using it repeatedly, this lets one know which call to the helper 
failed.
   
   Limited utility here, but a good habit to get into nonetheless.




-- 
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]


Reply via email to