Home | History | Annotate | Download | only in build
      1 // Copyright 2017 Google Inc. All rights reserved.
      2 //
      3 // Licensed under the Apache License, Version 2.0 (the "License");
      4 // you may not use this file except in compliance with the License.
      5 // You may obtain a copy of the License at
      6 //
      7 //     http://www.apache.org/licenses/LICENSE-2.0
      8 //
      9 // Unless required by applicable law or agreed to in writing, software
     10 // distributed under the License is distributed on an "AS IS" BASIS,
     11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     12 // See the License for the specific language governing permissions and
     13 // limitations under the License.
     14 
     15 package build
     16 
     17 import (
     18 	"reflect"
     19 	"strings"
     20 	"testing"
     21 )
     22 
     23 func TestEnvUnset(t *testing.T) {
     24 	initial := &Environment{"TEST=1", "TEST2=0"}
     25 	initial.Unset("TEST")
     26 	got := initial.Environ()
     27 	if len(got) != 1 || got[0] != "TEST2=0" {
     28 		t.Errorf("Expected [TEST2=0], got: %v", got)
     29 	}
     30 }
     31 
     32 func TestEnvUnsetMissing(t *testing.T) {
     33 	initial := &Environment{"TEST2=0"}
     34 	initial.Unset("TEST")
     35 	got := initial.Environ()
     36 	if len(got) != 1 || got[0] != "TEST2=0" {
     37 		t.Errorf("Expected [TEST2=0], got: %v", got)
     38 	}
     39 }
     40 
     41 func TestEnvSet(t *testing.T) {
     42 	initial := &Environment{}
     43 	initial.Set("TEST", "0")
     44 	got := initial.Environ()
     45 	if len(got) != 1 || got[0] != "TEST=0" {
     46 		t.Errorf("Expected [TEST=0], got: %v", got)
     47 	}
     48 }
     49 
     50 func TestEnvSetDup(t *testing.T) {
     51 	initial := &Environment{"TEST=1"}
     52 	initial.Set("TEST", "0")
     53 	got := initial.Environ()
     54 	if len(got) != 1 || got[0] != "TEST=0" {
     55 		t.Errorf("Expected [TEST=0], got: %v", got)
     56 	}
     57 }
     58 
     59 const testKatiEnvFileContents = `#!/bin/sh
     60 # Generated by kati unknown
     61 
     62 unset 'CLANG'
     63 export 'BUILD_ID'='NYC'
     64 `
     65 
     66 func TestEnvAppendFromKati(t *testing.T) {
     67 	initial := &Environment{"CLANG=/usr/bin/clang", "TEST=0"}
     68 	err := initial.appendFromKati(strings.NewReader(testKatiEnvFileContents))
     69 	if err != nil {
     70 		t.Fatalf("Unexpected error from %v", err)
     71 	}
     72 
     73 	got := initial.Environ()
     74 	expected := []string{"TEST=0", "BUILD_ID=NYC"}
     75 	if !reflect.DeepEqual(got, expected) {
     76 		t.Errorf("Environment list does not match")
     77 		t.Errorf("expected: %v", expected)
     78 		t.Errorf("     got: %v", got)
     79 	}
     80 }
     81