|
| 1 | +package export |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/stretchr/testify/assert" |
| 8 | + "github.com/stretchr/testify/require" |
| 9 | + |
| 10 | + "github.com/keboola/keboola-as-code/internal/pkg/filesystem" |
| 11 | + "github.com/keboola/keboola-as-code/internal/pkg/filesystem/aferofs" |
| 12 | + "github.com/keboola/keboola-as-code/internal/pkg/service/cli/dialog" |
| 13 | + "github.com/keboola/keboola-as-code/internal/pkg/service/cli/prompt" |
| 14 | + "github.com/keboola/keboola-as-code/internal/pkg/service/common/configmap" |
| 15 | +) |
| 16 | + |
| 17 | +func TestIsAllowedFile(t *testing.T) { |
| 18 | + t.Parallel() |
| 19 | + |
| 20 | + tests := []struct { |
| 21 | + name string |
| 22 | + filename string |
| 23 | + expected bool |
| 24 | + }{ |
| 25 | + // Allowed files |
| 26 | + {name: "keboola dir", filename: ".keboola", expected: true}, |
| 27 | + {name: "env file", filename: ".env", expected: true}, |
| 28 | + {name: "env local", filename: ".env.local", expected: true}, |
| 29 | + {name: "env dist", filename: ".env.dist", expected: true}, |
| 30 | + {name: "env custom", filename: ".env.production", expected: true}, |
| 31 | + {name: "gitignore", filename: ".gitignore", expected: true}, |
| 32 | + {name: "git dir", filename: ".git", expected: true}, |
| 33 | + |
| 34 | + // Not allowed files |
| 35 | + {name: "readme", filename: "README.md", expected: false}, |
| 36 | + {name: "src dir", filename: "src", expected: false}, |
| 37 | + {name: "go mod", filename: "go.mod", expected: false}, |
| 38 | + {name: "twin format", filename: "twin_format", expected: false}, |
| 39 | + {name: "main dir", filename: "main", expected: false}, |
| 40 | + {name: "hidden file", filename: ".hidden", expected: false}, |
| 41 | + {name: "env without dot prefix", filename: "env", expected: false}, |
| 42 | + } |
| 43 | + |
| 44 | + for _, tc := range tests { |
| 45 | + t.Run(tc.name, func(t *testing.T) { |
| 46 | + t.Parallel() |
| 47 | + result := isAllowedFile(tc.filename) |
| 48 | + assert.Equal(t, tc.expected, result, "isAllowedFile(%q) should be %v", tc.filename, tc.expected) |
| 49 | + }) |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +// mockPrompt implements prompt.Prompt for testing. |
| 54 | +type mockPrompt struct { |
| 55 | + confirmResult bool |
| 56 | +} |
| 57 | + |
| 58 | +func (m *mockPrompt) IsInteractive() bool { return true } |
| 59 | +func (m *mockPrompt) Printf(_ string, _ ...any) {} |
| 60 | +func (m *mockPrompt) Confirm(_ *prompt.Confirm) bool { return m.confirmResult } |
| 61 | +func (m *mockPrompt) Ask(q *prompt.Question) (string, bool) { return q.Default, true } |
| 62 | +func (m *mockPrompt) Select(s *prompt.Select) (string, bool) { return s.Default, true } |
| 63 | +func (m *mockPrompt) SelectIndex(s *prompt.SelectIndex) (int, bool) { return s.Default, true } |
| 64 | +func (m *mockPrompt) MultiSelect(s *prompt.MultiSelect) ([]string, bool) { return s.Default, true } |
| 65 | +func (m *mockPrompt) MultiSelectIndex(s *prompt.MultiSelectIndex) ([]int, bool) { |
| 66 | + return s.Default, true |
| 67 | +} |
| 68 | +func (m *mockPrompt) Multiline(q *prompt.Question) (string, bool) { return q.Default, true } |
| 69 | +func (m *mockPrompt) Editor(_ string, q *prompt.Question) (string, bool) { return q.Default, true } |
| 70 | + |
| 71 | +// mockValidateDeps implements validateDependencies for testing. |
| 72 | +type mockValidateDeps struct { |
| 73 | + fs filesystem.Fs |
| 74 | + dialogs *dialog.Dialogs |
| 75 | +} |
| 76 | + |
| 77 | +func (m *mockValidateDeps) Fs() filesystem.Fs { return m.fs } |
| 78 | +func (m *mockValidateDeps) Dialogs() *dialog.Dialogs { return m.dialogs } |
| 79 | + |
| 80 | +func TestValidateDirectory(t *testing.T) { |
| 81 | + t.Parallel() |
| 82 | + |
| 83 | + tests := []struct { |
| 84 | + name string |
| 85 | + files []string |
| 86 | + dirs []string |
| 87 | + force bool |
| 88 | + confirmResult bool |
| 89 | + expectError bool |
| 90 | + errorContains string |
| 91 | + }{ |
| 92 | + { |
| 93 | + name: "empty directory", |
| 94 | + files: nil, |
| 95 | + dirs: nil, |
| 96 | + force: false, |
| 97 | + expectError: false, |
| 98 | + }, |
| 99 | + { |
| 100 | + name: "only allowed files", |
| 101 | + files: []string{".env", ".env.local", ".gitignore"}, |
| 102 | + dirs: []string{".keboola", ".git"}, |
| 103 | + force: false, |
| 104 | + expectError: false, |
| 105 | + }, |
| 106 | + { |
| 107 | + name: "conflicts with force flag", |
| 108 | + files: []string{"README.md", "src"}, |
| 109 | + dirs: nil, |
| 110 | + force: true, |
| 111 | + expectError: false, |
| 112 | + }, |
| 113 | + { |
| 114 | + name: "conflicts without force, user confirms", |
| 115 | + files: []string{"README.md"}, |
| 116 | + dirs: nil, |
| 117 | + force: false, |
| 118 | + confirmResult: true, |
| 119 | + expectError: false, |
| 120 | + }, |
| 121 | + { |
| 122 | + name: "conflicts without force, user rejects", |
| 123 | + files: []string{"README.md"}, |
| 124 | + dirs: nil, |
| 125 | + force: false, |
| 126 | + confirmResult: false, |
| 127 | + expectError: true, |
| 128 | + errorContains: "export cancelled by user", |
| 129 | + }, |
| 130 | + { |
| 131 | + name: "mixed allowed and not allowed files with force", |
| 132 | + files: []string{".env", ".gitignore", "main.go", "go.mod"}, |
| 133 | + dirs: []string{".keboola"}, |
| 134 | + force: true, |
| 135 | + expectError: false, |
| 136 | + }, |
| 137 | + { |
| 138 | + name: "mixed files without force, user rejects", |
| 139 | + files: []string{".env", "main.go"}, |
| 140 | + dirs: nil, |
| 141 | + force: false, |
| 142 | + confirmResult: false, |
| 143 | + expectError: true, |
| 144 | + errorContains: "export cancelled by user", |
| 145 | + }, |
| 146 | + } |
| 147 | + |
| 148 | + for _, tc := range tests { |
| 149 | + t.Run(tc.name, func(t *testing.T) { |
| 150 | + t.Parallel() |
| 151 | + ctx := context.Background() |
| 152 | + |
| 153 | + // Create memory filesystem |
| 154 | + fs := aferofs.NewMemoryFs() |
| 155 | + |
| 156 | + // Create test files |
| 157 | + for _, f := range tc.files { |
| 158 | + require.NoError(t, fs.WriteFile(ctx, filesystem.NewRawFile(f, "test content"))) |
| 159 | + } |
| 160 | + |
| 161 | + // Create test directories |
| 162 | + for _, d := range tc.dirs { |
| 163 | + require.NoError(t, fs.Mkdir(ctx, d)) |
| 164 | + } |
| 165 | + |
| 166 | + // Create mock dependencies |
| 167 | + mockP := &mockPrompt{confirmResult: tc.confirmResult} |
| 168 | + deps := &mockValidateDeps{ |
| 169 | + fs: fs, |
| 170 | + dialogs: dialog.New(mockP), |
| 171 | + } |
| 172 | + |
| 173 | + // Create flags |
| 174 | + flags := Flags{ |
| 175 | + Force: configmap.Value[bool]{Value: tc.force}, |
| 176 | + } |
| 177 | + |
| 178 | + // Run validation |
| 179 | + err := validateDirectory(ctx, deps, flags) |
| 180 | + |
| 181 | + // Check result |
| 182 | + if tc.expectError { |
| 183 | + require.Error(t, err) |
| 184 | + if tc.errorContains != "" { |
| 185 | + assert.Contains(t, err.Error(), tc.errorContains) |
| 186 | + } |
| 187 | + } else { |
| 188 | + require.NoError(t, err) |
| 189 | + } |
| 190 | + }) |
| 191 | + } |
| 192 | +} |
0 commit comments