(t *testing.T)
| 1300 | } |
| 1301 | |
| 1302 | func TestReadFileLines(t *testing.T) { |
| 1303 | t.Parallel() |
| 1304 | |
| 1305 | tmpdir := os.TempDir() |
| 1306 | noPermsFilePath := filepath.Join(tmpdir, "no-perms-lines") |
| 1307 | |
| 1308 | logger := slogtest.Make(t, &slogtest.Options{IgnoreErrors: true}).Leveled(slog.LevelDebug) |
| 1309 | fs := newTestFs(afero.NewMemMapFs(), func(call, file string) error { |
| 1310 | if file == noPermsFilePath { |
| 1311 | return os.ErrPermission |
| 1312 | } |
| 1313 | return nil |
| 1314 | }) |
| 1315 | api := agentfiles.NewAPI(logger, fs, nil) |
| 1316 | |
| 1317 | dirPath := filepath.Join(tmpdir, "a-directory-lines") |
| 1318 | err := fs.MkdirAll(dirPath, 0o755) |
| 1319 | require.NoError(t, err) |
| 1320 | |
| 1321 | emptyFilePath := filepath.Join(tmpdir, "empty-file") |
| 1322 | err = afero.WriteFile(fs, emptyFilePath, []byte(""), 0o644) |
| 1323 | require.NoError(t, err) |
| 1324 | |
| 1325 | basicFilePath := filepath.Join(tmpdir, "basic-file") |
| 1326 | err = afero.WriteFile(fs, basicFilePath, []byte("line1\nline2\nline3"), 0o644) |
| 1327 | require.NoError(t, err) |
| 1328 | |
| 1329 | longLine := string(bytes.Repeat([]byte("x"), 1025)) |
| 1330 | longLineFilePath := filepath.Join(tmpdir, "long-line-file") |
| 1331 | err = afero.WriteFile(fs, longLineFilePath, []byte(longLine), 0o644) |
| 1332 | require.NoError(t, err) |
| 1333 | |
| 1334 | largeFilePath := filepath.Join(tmpdir, "large-file") |
| 1335 | err = afero.WriteFile(fs, largeFilePath, bytes.Repeat([]byte("x"), 1<<20+1), 0o644) |
| 1336 | require.NoError(t, err) |
| 1337 | |
| 1338 | tests := []struct { |
| 1339 | name string |
| 1340 | path string |
| 1341 | offset int64 |
| 1342 | limit int64 |
| 1343 | expSuccess bool |
| 1344 | expError string |
| 1345 | expContent string |
| 1346 | expTotal int |
| 1347 | expRead int |
| 1348 | expSize int64 |
| 1349 | // useCodersdk is set for cases where the handler returns |
| 1350 | // codersdk.Response (query param validation) instead of ReadFileLinesResponse. |
| 1351 | useCodersdk bool |
| 1352 | }{ |
| 1353 | { |
| 1354 | name: "NoPath", |
| 1355 | path: "", |
| 1356 | useCodersdk: true, |
| 1357 | expError: "is required", |
| 1358 | }, |
| 1359 | { |
nothing calls this directly
no test coverage detected