github.com/crossplane-contrib/function-cue@v0.2.2-0.20240508161918-5100fcb5a058/internal/cuetools/tester_test.go (about) 1 // Licensed to Elasticsearch B.V. under one or more contributor 2 // license agreements. See the NOTICE file distributed with 3 // this work for additional information regarding copyright 4 // ownership. Elasticsearch B.V. licenses this file to you under 5 // the Apache License, Version 2.0 (the "License"); you may 6 // not use this file except in compliance with the License. 7 // You may obtain a copy of the License at 8 // 9 // http://www.apache.org/licenses/LICENSE-2.0 10 // 11 // Unless required by applicable law or agreed to in writing, 12 // software distributed under the License is distributed on an 13 // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 // KIND, either express or implied. See the License for the 15 // specific language governing permissions and limitations 16 // under the License. 17 18 package cuetools 19 20 import ( 21 "bytes" 22 "os" 23 "strings" 24 "testing" 25 26 "github.com/stretchr/testify/assert" 27 "github.com/stretchr/testify/require" 28 ) 29 30 func getOutput() (_ *bytes.Buffer, reset func()) { 31 old := TestOutput 32 buf := bytes.NewBuffer(nil) 33 TestOutput = buf 34 return buf, func() { 35 TestOutput = old 36 } 37 } 38 39 func chdirCueRoot(t *testing.T) func() { 40 old, err := os.Getwd() 41 require.NoError(t, err) 42 err = os.Chdir("testdata") 43 require.NoError(t, err) 44 return func() { 45 err = os.Chdir(old) 46 require.NoError(t, err) 47 } 48 } 49 50 func TestTester(t *testing.T) { 51 fn := chdirCueRoot(t) 52 defer fn() 53 buf, reset := getOutput() 54 defer reset() 55 tester, err := NewTester(TestConfig{ 56 Package: "./runtime", 57 }) 58 require.NoError(t, err) 59 envDiff := ExternalDiffEnvVar 60 diffProgram := os.Getenv(envDiff) 61 if diffProgram != "" { 62 err = os.Unsetenv(envDiff) // we expect a specific diff format 63 require.NoError(t, err) 64 defer func() { _ = os.Setenv(envDiff, diffProgram) }() 65 } 66 err = tester.Run() 67 expected := ` 68 running test tags: correct, incorrect 69 > run test "correct" 70 PASS correct 71 > run test "incorrect" 72 diffs found: 73 --- expected 74 +++ actual 75 @@ -3,5 +3,5 @@ 76 main: 77 resource: 78 bar: baz 79 - foo: bar 80 + foo: foo2 81 FAIL incorrect: expected did not match actual 82 ` 83 require.Error(t, err) 84 assert.Equal(t, "1 of 2 tests had errors", err.Error()) 85 assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(buf.String())) 86 } 87 88 func TestTesterLegacyOptions(t *testing.T) { 89 fn := chdirCueRoot(t) 90 defer fn() 91 buf, reset := getOutput() 92 defer reset() 93 tester, err := NewTester(TestConfig{ 94 Package: "./runtime2", 95 RequestVar: "_request", 96 ResponseVar: ".", 97 LegacyDesiredOnlyResponse: true, 98 }) 99 require.NoError(t, err) 100 envDiff := ExternalDiffEnvVar 101 diffProgram := os.Getenv(envDiff) 102 if diffProgram != "" { 103 err = os.Unsetenv(envDiff) // we expect a specific diff format 104 require.NoError(t, err) 105 defer func() { _ = os.Setenv(envDiff, diffProgram) }() 106 } 107 err = tester.Run() 108 expected := ` 109 running test tags: correct, incorrect 110 > run test "correct" 111 PASS correct 112 > run test "incorrect" 113 diffs found: 114 --- expected 115 +++ actual 116 @@ -3,5 +3,5 @@ 117 main: 118 resource: 119 bar: baz 120 - foo: bar 121 + foo: foo2 122 FAIL incorrect: expected did not match actual 123 ` 124 require.Error(t, err) 125 assert.Equal(t, "1 of 2 tests had errors", err.Error()) 126 assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(buf.String())) 127 } 128 129 func TestTesterBadTag(t *testing.T) { 130 fn := chdirCueRoot(t) 131 defer fn() 132 buf, reset := getOutput() 133 defer reset() 134 tester, err := NewTester(TestConfig{ 135 Package: "./runtime", 136 TestTags: []string{"foo"}, 137 }) 138 require.NoError(t, err) 139 err = tester.Run() 140 require.Error(t, err) 141 assert.Equal(t, "1 of 1 tests had errors", err.Error()) 142 assert.Contains(t, buf.String(), "FAIL foo: evaluate expected: load instance: build constraints exclude all CUE files in ./runtime/tests") 143 } 144 145 func TestTesterAllPass(t *testing.T) { 146 fn := chdirCueRoot(t) 147 defer fn() 148 buf, reset := getOutput() 149 defer reset() 150 tester, err := NewTester(TestConfig{ 151 Package: "./runtime", 152 TestTags: []string{"correct"}, 153 }) 154 require.NoError(t, err) 155 err = tester.Run() 156 require.NoError(t, err) 157 assert.Contains(t, buf.String(), "PASS correct") 158 }