Skip to content

Commit 78333b4

Browse files
authored
Merge pull request #2628 from cuipinghuo/add-ut-for-intenal-validate
test: add unit test coverage for functions in internal/validate path
2 parents d771895 + 4cd1d32 commit 78333b4

File tree

1 file changed

+98
-0
lines changed

1 file changed

+98
-0
lines changed

internal/validate/helpers_test.go

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
// Copyright The Conforma Contributors
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+
// SPDX-License-Identifier: Apache-2.0
16+
package validate
17+
18+
import (
19+
"context"
20+
"testing"
21+
22+
"github.com/spf13/afero"
23+
"github.com/stretchr/testify/assert"
24+
"github.com/stretchr/testify/require"
25+
26+
"github.com/conforma/cli/internal/utils"
27+
)
28+
29+
func TestGetPolicyConfig(t *testing.T) {
30+
fs := afero.NewMemMapFs()
31+
ctx := utils.WithFS(context.Background(), fs)
32+
fileName := "/tmp/policy.yaml"
33+
fileContent := "foo: bar"
34+
emptyFile := "/tmp/empty.yaml"
35+
err := afero.WriteFile(fs, fileName, []byte(fileContent), 0644)
36+
require.NoError(t, err)
37+
err = afero.WriteFile(fs, emptyFile, []byte{}, 0644)
38+
require.NoError(t, err)
39+
40+
tests := []struct {
41+
name string
42+
input string
43+
want string
44+
wantErr bool
45+
errMsg string
46+
}{
47+
{"inline string", "{\"foo\": \"bar\"}", "{\"foo\": \"bar\"}", false, ""},
48+
{"file", fileName, fileContent, false, ""},
49+
{"empty file", emptyFile, "", true, "empty"},
50+
}
51+
52+
for _, tt := range tests {
53+
t.Run(tt.name, func(t *testing.T) {
54+
result, err := GetPolicyConfig(ctx, tt.input)
55+
if tt.wantErr {
56+
assert.Error(t, err)
57+
assert.Contains(t, err.Error(), tt.errMsg)
58+
} else {
59+
assert.NoError(t, err)
60+
assert.Equal(t, tt.want, result)
61+
}
62+
})
63+
}
64+
}
65+
66+
func TestReadFile(t *testing.T) {
67+
fs := afero.NewMemMapFs()
68+
ctx := utils.WithFS(context.Background(), fs)
69+
err := afero.WriteFile(fs, "/tmp/testfile.txt", []byte("hello world"), 0644)
70+
require.NoError(t, err)
71+
err = afero.WriteFile(fs, "/tmp/emptyfile.txt", []byte{}, 0644)
72+
require.NoError(t, err)
73+
74+
tests := []struct {
75+
name string
76+
file string
77+
want string
78+
wantErr bool
79+
errMsg string
80+
}{
81+
{"valid file", "/tmp/testfile.txt", "hello world", false, ""},
82+
{"empty file", "/tmp/emptyfile.txt", "", true, "empty"},
83+
{"missing file", "/tmp/doesnotexist.txt", "", true, "file does not exist"},
84+
}
85+
86+
for _, tt := range tests {
87+
t.Run(tt.name, func(t *testing.T) {
88+
got, err := ReadFile(ctx, tt.file)
89+
if tt.wantErr {
90+
assert.Error(t, err)
91+
assert.Contains(t, err.Error(), tt.errMsg)
92+
} else {
93+
assert.NoError(t, err)
94+
assert.Equal(t, tt.want, got)
95+
}
96+
})
97+
}
98+
}

0 commit comments

Comments
 (0)