github.com/ouraigua/jenkins-library@v0.0.0-20231028010029-fbeaf2f3aa9b/pkg/gcs/gcsClient_test.go (about)

     1  //go:build unit
     2  // +build unit
     3  
     4  package gcs
     5  
     6  import (
     7  	"os"
     8  	"testing"
     9  )
    10  
    11  func Test_gcsClient_prepareEnv(t *testing.T) {
    12  	os.Setenv("TESTVAR1", "test1")
    13  
    14  	gcsClient := gcsClient{envVars: []EnvVar{
    15  		{Name: "TESTVAR1", Value: "test1_new"},
    16  		{Name: "TESTVAR2", Value: "test2_new"},
    17  	}}
    18  
    19  	gcsClient.prepareEnv()
    20  
    21  	if gcsClient.envVars[0].Modified {
    22  		t.Errorf("%v - expected '%v' was '%v'", gcsClient.envVars[0].Name, false, gcsClient.envVars[0].Modified)
    23  	}
    24  	if !gcsClient.envVars[1].Modified {
    25  		t.Errorf("%v - expected '%v' was '%v'", gcsClient.envVars[1].Name, true, gcsClient.envVars[1].Modified)
    26  	}
    27  
    28  	os.Setenv("TESTVAR1", "")
    29  	os.Setenv("TESTVAR2", "")
    30  }
    31  
    32  func TestCleanupEnv(t *testing.T) {
    33  	os.Setenv("TESTVAR1", "test1")
    34  	os.Setenv("TESTVAR2", "test2")
    35  
    36  	gcsClient := gcsClient{envVars: []EnvVar{
    37  		{Name: "TESTVAR1", Modified: false},
    38  		{Name: "TESTVAR2", Modified: true},
    39  	}}
    40  
    41  	gcsClient.cleanupEnv()
    42  
    43  	if os.Getenv("TESTVAR1") != "test1" {
    44  		t.Errorf("%v - expected '%v' was '%v'", gcsClient.envVars[0].Name, "test1", os.Getenv("TESTVAR1"))
    45  	}
    46  	if len(os.Getenv("TESTVAR2")) > 0 {
    47  		t.Errorf("%v - expected '%v' was '%v'", gcsClient.envVars[1].Name, "", os.Getenv("TESTVAR2"))
    48  	}
    49  
    50  	os.Setenv("TESTVAR1", "")
    51  	os.Setenv("TESTVAR2", "")
    52  }