Skip to content

Commit 96277c6

Browse files
committed
create
Signed-off-by: Sinelnikov Michail <mikhail.sinelnikov@flant.com>
1 parent 8aac016 commit 96277c6

File tree

6 files changed

+116
-0
lines changed

6 files changed

+116
-0
lines changed

internal/werf/files.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,3 +99,15 @@ func (f files) Glob(pattern string) map[string]any {
9999
return res
100100
}
101101
}
102+
103+
// Exists returns true if the named file exists in the Files object.
104+
//
105+
// This is designed to be called from a template.
106+
//
107+
// {{ if .Files.Exists "foo" }}
108+
// {{ .Files.Get "foo" }}
109+
// {{ end }}
110+
func (f files) Exists(relPath string) bool {
111+
fullPath := filepath.Join(f.rootDir, relPath)
112+
return fsutils.IsFile(fullPath)
113+
}

internal/werf/files_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,3 +170,28 @@ func TestGlobWithWerfIncYaml(t *testing.T) {
170170
require.Len(t, result, 1)
171171
require.Equal(t, "root module yaml", result["werf.yaml"])
172172
}
173+
174+
func TestExists(t *testing.T) {
175+
rootDir, moduleDir, cleanup := setupTestEnvironment(t)
176+
defer cleanup()
177+
178+
f := NewFiles(rootDir, moduleDir)
179+
180+
// Test existing files
181+
require.True(t, f.Exists("test.txt"), "existing file test.txt should return true")
182+
require.True(t, f.Exists("dir1/file1.txt"), "existing file dir1/file1.txt should return true")
183+
require.True(t, f.Exists("werf.yaml"), "existing file werf.yaml should return true")
184+
185+
// Test non-existing files
186+
require.False(t, f.Exists("non-existent.txt"), "non-existent file should return false")
187+
require.False(t, f.Exists("dir1/non-existent.txt"), "non-existent file in existing dir should return false")
188+
require.False(t, f.Exists("non-existent-dir/file.txt"), "file in non-existent dir should return false")
189+
190+
// Test special case for base_images.yml
191+
require.False(t, f.Exists("base_images.yml"), "base_images.yml should return false when file doesn't exist")
192+
require.False(t, f.Exists("base_images.yaml"), "base_images.yaml should return false when file doesn't exist")
193+
194+
// Test directories (should return false)
195+
require.False(t, f.Exists("dir1"), "directory should return false")
196+
require.False(t, f.Exists("dir3"), "directory should return false")
197+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Test werf config with Files.Exists usage
2+
{{- $Root := . }}
3+
{{- if $Root.Files.Exists "modules/021-cni-cilium/module.yaml" }}
4+
# Module exists, include it
5+
module-cni-cilium: exists
6+
{{- else }}
7+
# Module doesn't exist
8+
module-cni-cilium: missing
9+
{{- end }}
10+
11+
{{- if $Root.Files.Exists "nonexistent-module/module.yaml" }}
12+
# Non-existent module
13+
nonexistent: exists
14+
{{- else }}
15+
# Non-existent module
16+
nonexistent: missing
17+
{{- end }}
18+
19+
{{- if .Files.Exists "nonexistent-module/module.yaml" }}
20+
# Non-existent module
21+
nonexistent: exists
22+
{{- else }}
23+
# Non-existent module
24+
nonexistent: missing
25+
{{- end }}
26+
27+
{{- if .Files.Exists "nonexistent-module/module.yaml" }}
28+
# Non-existent module
29+
nonexistent: exists
30+
{{- else }}
31+
# Non-existent module
32+
nonexistent: missing
33+
{{- end }}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
name: 021-cni-cilium
2+
namespace: d8-system
3+
weight: 210
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
{{- $content := .Files.Get ".werf/test-werf-modules.yaml" }}
2+
{{- tpl $content . }}

internal/werf/werf_test.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package werf
1818

1919
import (
2020
"path/filepath"
21+
"strings"
2122
"testing"
2223
)
2324

@@ -50,3 +51,43 @@ func TestGetWerfConfig(t *testing.T) {
5051
})
5152
}
5253
}
54+
55+
func TestGetWerfConfigWithFilesExists(t *testing.T) {
56+
tests := []struct {
57+
name string
58+
dir string
59+
expectedContains []string
60+
unexpectedContains []string
61+
wantErr bool
62+
}{
63+
{
64+
name: "Test werf.yaml with Files.Exists",
65+
dir: "testdata",
66+
expectedContains: []string{"module-cni-cilium: exists", "nonexistent: missing"},
67+
unexpectedContains: []string{"module-cni-cilium: missing", "nonexistent: exists"},
68+
wantErr: false,
69+
},
70+
}
71+
72+
for _, tt := range tests {
73+
t.Run(tt.name, func(t *testing.T) {
74+
config, err := GetWerfConfig(tt.dir)
75+
if (err != nil) != tt.wantErr {
76+
t.Errorf("GetWerfConfig() error = %v, wantErr %v", err, tt.wantErr)
77+
return
78+
}
79+
if !tt.wantErr {
80+
for _, expected := range tt.expectedContains {
81+
if !strings.Contains(config, expected) {
82+
t.Errorf("GetWerfConfig() config = %v, expected to contain %v", config, expected)
83+
}
84+
}
85+
for _, unexpected := range tt.unexpectedContains {
86+
if strings.Contains(config, unexpected) {
87+
t.Errorf("GetWerfConfig() config = %v, should not contain %v", config, unexpected)
88+
}
89+
}
90+
}
91+
})
92+
}
93+
}

0 commit comments

Comments
 (0)