MCPcopy Index your code
hub / github.com/coder/coder / TestScanRespectsGitignore

Function TestScanRespectsGitignore

agent/agentgit/agentgit_test.go:150–186  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

148}
149
150func TestScanRespectsGitignore(t *testing.T) {
151 t.Parallel()
152
153 repoDir := initTestRepo(t)
154 logger := slogtest.Make(t, nil)
155
156 // Add a .gitignore that ignores *.log files and the build/ directory.
157 require.NoError(t, os.WriteFile(filepath.Join(repoDir, ".gitignore"), []byte("*.log\nbuild/\n"), 0o600))
158 gitCmd(t, repoDir, "add", ".gitignore")
159 gitCmd(t, repoDir, "commit", "-m", "add gitignore")
160
161 // Create unstaged files: two normal, three matching gitignore patterns.
162 require.NoError(t, os.WriteFile(filepath.Join(repoDir, "main.go"), []byte("package main\n"), 0o600))
163 require.NoError(t, os.WriteFile(filepath.Join(repoDir, "util.go"), []byte("package util\n"), 0o600))
164 require.NoError(t, os.WriteFile(filepath.Join(repoDir, "debug.log"), []byte("some log output\n"), 0o600))
165 require.NoError(t, os.WriteFile(filepath.Join(repoDir, "error.log"), []byte("some error\n"), 0o600))
166 require.NoError(t, os.MkdirAll(filepath.Join(repoDir, "build"), 0o700))
167 require.NoError(t, os.WriteFile(filepath.Join(repoDir, "build", "output.bin"), []byte("binary\n"), 0o600))
168
169 h := agentgit.NewHandler(logger)
170 h.Subscribe([]string{filepath.Join(repoDir, "main.go")})
171
172 ctx := context.Background()
173 msg := h.Scan(ctx)
174 require.NotNil(t, msg)
175 require.Len(t, msg.Repositories, 1)
176
177 diff := msg.Repositories[0].UnifiedDiff
178
179 // The non-ignored files should appear in the diff.
180 assert.Contains(t, diff, "main.go")
181 assert.Contains(t, diff, "util.go")
182 // The gitignored files must not appear in the diff.
183 assert.NotContains(t, diff, "debug.log")
184 assert.NotContains(t, diff, "error.log")
185 assert.NotContains(t, diff, "output.bin")
186}
187
188func TestScanRespectsGitignoreNestedNegation(t *testing.T) {
189 t.Parallel()

Callers

nothing calls this directly

Calls 9

SubscribeMethod · 0.95
ScanMethod · 0.95
NewHandlerFunction · 0.92
initTestRepoFunction · 0.85
gitCmdFunction · 0.85
MkdirAllMethod · 0.80
WriteFileMethod · 0.65
LenMethod · 0.45
ContainsMethod · 0.45

Tested by

no test coverage detected