|
| 1 | +package installer |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | +) |
| 6 | + |
| 7 | +func TestTrimWhitespace(t *testing.T) { |
| 8 | + testCases := []struct { |
| 9 | + input string |
| 10 | + expected string |
| 11 | + }{ |
| 12 | + {"Nothing to trim", "Nothing to trim"}, |
| 13 | + {" \n\nOne Two \n ", "One Two"}, |
| 14 | + } |
| 15 | + |
| 16 | + for _, testCase := range testCases { |
| 17 | + actual := trimWhitespace([]byte(testCase.input)) |
| 18 | + |
| 19 | + if string(actual) != testCase.expected { |
| 20 | + t.Errorf("Expected string = %s; got string = %s", testCase.expected, actual) |
| 21 | + } |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +func TestTrimShebang(t *testing.T) { |
| 26 | + testCases := []struct { |
| 27 | + input string |
| 28 | + expected string |
| 29 | + }{ |
| 30 | + {"#!/bin/bash\ncode", "code"}, |
| 31 | + {"code\n#!/bin/bash\ncode", "code\n#!/bin/bash\ncode"}, |
| 32 | + } |
| 33 | + |
| 34 | + for _, testCase := range testCases { |
| 35 | + actual := trimShebang([]byte(testCase.input)) |
| 36 | + |
| 37 | + if string(actual) != testCase.expected { |
| 38 | + t.Errorf("Expected string = %s; got string = %s", testCase.expected, actual) |
| 39 | + } |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +func TestExpandEnvironment(t *testing.T) { |
| 44 | + envMap := map[string]string{ |
| 45 | + "VARIABLE": "myVariable", |
| 46 | + } |
| 47 | + |
| 48 | + testCases := []struct { |
| 49 | + input string |
| 50 | + expected string |
| 51 | + }{ |
| 52 | + {"Testing ${VARIABLE}", "Testing myVariable"}, |
| 53 | + {"testing ${INVALID}", "testing "}, |
| 54 | + } |
| 55 | + |
| 56 | + origEnvGetter := envGetter |
| 57 | + defer func() { envGetter = origEnvGetter }() |
| 58 | + |
| 59 | + envGetter = func(key string) string { |
| 60 | + return envMap[key] |
| 61 | + } |
| 62 | + |
| 63 | + for _, testCase := range testCases { |
| 64 | + actual := expandEnvironment([]byte(testCase.input)) |
| 65 | + |
| 66 | + if string(actual) != testCase.expected { |
| 67 | + t.Errorf("Expected string = %s; got string = %s", testCase.expected, actual) |
| 68 | + } |
| 69 | + } |
| 70 | +} |
0 commit comments