blob: 37f500f3043895a8459a559cb1e99fa0d3e04e74 [file] [log] [blame]
Dan Willemsen1e704462016-08-21 15:17:17 -07001// 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
15package build
16
17import (
18 "reflect"
19 "strings"
20 "testing"
21)
22
23func 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
32func 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
41func 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
50func 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
Dan Willemsenfb1271a2018-09-26 15:00:42 -070059func TestEnvAllow(t *testing.T) {
60 initial := &Environment{"TEST=1", "TEST2=0", "TEST3=2"}
61 initial.Allow("TEST3", "TEST")
62 got := initial.Environ()
63 if len(got) != 2 || got[0] != "TEST=1" || got[1] != "TEST3=2" {
64 t.Errorf("Expected [TEST=1 TEST3=2], got: %v", got)
65 }
66}
67
Dan Willemsen1e704462016-08-21 15:17:17 -070068const testKatiEnvFileContents = `#!/bin/sh
69# Generated by kati unknown
70
71unset 'CLANG'
72export 'BUILD_ID'='NYC'
73`
74
75func TestEnvAppendFromKati(t *testing.T) {
Dan Willemsen1e704462016-08-21 15:17:17 -070076 initial := &Environment{"CLANG=/usr/bin/clang", "TEST=0"}
77 err := initial.appendFromKati(strings.NewReader(testKatiEnvFileContents))
78 if err != nil {
79 t.Fatalf("Unexpected error from %v", err)
80 }
81
82 got := initial.Environ()
83 expected := []string{"TEST=0", "BUILD_ID=NYC"}
84 if !reflect.DeepEqual(got, expected) {
85 t.Errorf("Environment list does not match")
86 t.Errorf("expected: %v", expected)
87 t.Errorf(" got: %v", got)
88 }
89}