|
6 | 6 | "io/ioutil" |
7 | 7 | "os" |
8 | 8 | "path/filepath" |
| 9 | + "regexp" |
9 | 10 | "runtime" |
10 | 11 |
|
11 | 12 | "gopkg.in/src-d/go-git.v4/config" |
@@ -1317,3 +1318,187 @@ func (s *WorktreeSuite) TestAlternatesRepo(c *C) { |
1317 | 1318 |
|
1318 | 1319 | c.Assert(commit1.String(), Equals, commit2.String()) |
1319 | 1320 | } |
| 1321 | + |
| 1322 | +func (s *WorktreeSuite) TestGrep(c *C) { |
| 1323 | + cases := []struct { |
| 1324 | + name string |
| 1325 | + options GrepOptions |
| 1326 | + wantResult []GrepResult |
| 1327 | + dontWantResult []GrepResult |
| 1328 | + wantError error |
| 1329 | + }{ |
| 1330 | + { |
| 1331 | + name: "basic word match", |
| 1332 | + options: GrepOptions{ |
| 1333 | + Pattern: regexp.MustCompile("import"), |
| 1334 | + }, |
| 1335 | + wantResult: []GrepResult{ |
| 1336 | + { |
| 1337 | + FileName: "go/example.go", |
| 1338 | + LineNumber: 3, |
| 1339 | + Content: "import (", |
| 1340 | + TreeName: "6ecf0ef2c2dffb796033e5a02219af86ec6584e5", |
| 1341 | + }, |
| 1342 | + { |
| 1343 | + FileName: "vendor/foo.go", |
| 1344 | + LineNumber: 3, |
| 1345 | + Content: "import \"fmt\"", |
| 1346 | + TreeName: "6ecf0ef2c2dffb796033e5a02219af86ec6584e5", |
| 1347 | + }, |
| 1348 | + }, |
| 1349 | + }, { |
| 1350 | + name: "case insensitive match", |
| 1351 | + options: GrepOptions{ |
| 1352 | + Pattern: regexp.MustCompile(`(?i)IMport`), |
| 1353 | + }, |
| 1354 | + wantResult: []GrepResult{ |
| 1355 | + { |
| 1356 | + FileName: "go/example.go", |
| 1357 | + LineNumber: 3, |
| 1358 | + Content: "import (", |
| 1359 | + TreeName: "6ecf0ef2c2dffb796033e5a02219af86ec6584e5", |
| 1360 | + }, |
| 1361 | + { |
| 1362 | + FileName: "vendor/foo.go", |
| 1363 | + LineNumber: 3, |
| 1364 | + Content: "import \"fmt\"", |
| 1365 | + TreeName: "6ecf0ef2c2dffb796033e5a02219af86ec6584e5", |
| 1366 | + }, |
| 1367 | + }, |
| 1368 | + }, { |
| 1369 | + name: "invert match", |
| 1370 | + options: GrepOptions{ |
| 1371 | + Pattern: regexp.MustCompile("import"), |
| 1372 | + InvertMatch: true, |
| 1373 | + }, |
| 1374 | + dontWantResult: []GrepResult{ |
| 1375 | + { |
| 1376 | + FileName: "go/example.go", |
| 1377 | + LineNumber: 3, |
| 1378 | + Content: "import (", |
| 1379 | + TreeName: "6ecf0ef2c2dffb796033e5a02219af86ec6584e5", |
| 1380 | + }, |
| 1381 | + { |
| 1382 | + FileName: "vendor/foo.go", |
| 1383 | + LineNumber: 3, |
| 1384 | + Content: "import \"fmt\"", |
| 1385 | + TreeName: "6ecf0ef2c2dffb796033e5a02219af86ec6584e5", |
| 1386 | + }, |
| 1387 | + }, |
| 1388 | + }, { |
| 1389 | + name: "match at a given commit hash", |
| 1390 | + options: GrepOptions{ |
| 1391 | + Pattern: regexp.MustCompile("The MIT License"), |
| 1392 | + CommitHash: plumbing.NewHash("b029517f6300c2da0f4b651b8642506cd6aaf45d"), |
| 1393 | + }, |
| 1394 | + wantResult: []GrepResult{ |
| 1395 | + { |
| 1396 | + FileName: "LICENSE", |
| 1397 | + LineNumber: 1, |
| 1398 | + Content: "The MIT License (MIT)", |
| 1399 | + TreeName: "b029517f6300c2da0f4b651b8642506cd6aaf45d", |
| 1400 | + }, |
| 1401 | + }, |
| 1402 | + dontWantResult: []GrepResult{ |
| 1403 | + { |
| 1404 | + FileName: "go/example.go", |
| 1405 | + LineNumber: 3, |
| 1406 | + Content: "import (", |
| 1407 | + TreeName: "6ecf0ef2c2dffb796033e5a02219af86ec6584e5", |
| 1408 | + }, |
| 1409 | + }, |
| 1410 | + }, { |
| 1411 | + name: "match for a given pathspec", |
| 1412 | + options: GrepOptions{ |
| 1413 | + Pattern: regexp.MustCompile("import"), |
| 1414 | + PathSpec: regexp.MustCompile("go/"), |
| 1415 | + }, |
| 1416 | + wantResult: []GrepResult{ |
| 1417 | + { |
| 1418 | + FileName: "go/example.go", |
| 1419 | + LineNumber: 3, |
| 1420 | + Content: "import (", |
| 1421 | + TreeName: "6ecf0ef2c2dffb796033e5a02219af86ec6584e5", |
| 1422 | + }, |
| 1423 | + }, |
| 1424 | + dontWantResult: []GrepResult{ |
| 1425 | + { |
| 1426 | + FileName: "vendor/foo.go", |
| 1427 | + LineNumber: 3, |
| 1428 | + Content: "import \"fmt\"", |
| 1429 | + TreeName: "6ecf0ef2c2dffb796033e5a02219af86ec6584e5", |
| 1430 | + }, |
| 1431 | + }, |
| 1432 | + }, { |
| 1433 | + name: "match at a given reference name", |
| 1434 | + options: GrepOptions{ |
| 1435 | + Pattern: regexp.MustCompile("import"), |
| 1436 | + ReferenceName: "refs/heads/master", |
| 1437 | + }, |
| 1438 | + wantResult: []GrepResult{ |
| 1439 | + { |
| 1440 | + FileName: "go/example.go", |
| 1441 | + LineNumber: 3, |
| 1442 | + Content: "import (", |
| 1443 | + TreeName: "refs/heads/master", |
| 1444 | + }, |
| 1445 | + }, |
| 1446 | + }, { |
| 1447 | + name: "ambiguous options", |
| 1448 | + options: GrepOptions{ |
| 1449 | + Pattern: regexp.MustCompile("import"), |
| 1450 | + CommitHash: plumbing.NewHash("2d55a722f3c3ecc36da919dfd8b6de38352f3507"), |
| 1451 | + ReferenceName: "somereferencename", |
| 1452 | + }, |
| 1453 | + wantError: ErrHashOrReference, |
| 1454 | + }, |
| 1455 | + } |
| 1456 | + |
| 1457 | + path := fixtures.Basic().ByTag("worktree").One().Worktree().Root() |
| 1458 | + server, err := PlainClone(c.MkDir(), false, &CloneOptions{ |
| 1459 | + URL: path, |
| 1460 | + }) |
| 1461 | + c.Assert(err, IsNil) |
| 1462 | + |
| 1463 | + w, err := server.Worktree() |
| 1464 | + c.Assert(err, IsNil) |
| 1465 | + |
| 1466 | + for _, tc := range cases { |
| 1467 | + gr, err := w.Grep(&tc.options) |
| 1468 | + if tc.wantError != nil { |
| 1469 | + c.Assert(err, Equals, tc.wantError) |
| 1470 | + } else { |
| 1471 | + c.Assert(err, IsNil) |
| 1472 | + } |
| 1473 | + |
| 1474 | + // Iterate through the results and check if the wanted result is present |
| 1475 | + // in the got result. |
| 1476 | + for _, wantResult := range tc.wantResult { |
| 1477 | + found := false |
| 1478 | + for _, gotResult := range gr { |
| 1479 | + if wantResult == gotResult { |
| 1480 | + found = true |
| 1481 | + break |
| 1482 | + } |
| 1483 | + } |
| 1484 | + if found != true { |
| 1485 | + c.Errorf("unexpected grep results for %q, expected result to contain: %v", tc.name, wantResult) |
| 1486 | + } |
| 1487 | + } |
| 1488 | + |
| 1489 | + // Iterate through the results and check if the not wanted result is |
| 1490 | + // present in the got result. |
| 1491 | + for _, dontWantResult := range tc.dontWantResult { |
| 1492 | + found := false |
| 1493 | + for _, gotResult := range gr { |
| 1494 | + if dontWantResult == gotResult { |
| 1495 | + found = true |
| 1496 | + break |
| 1497 | + } |
| 1498 | + } |
| 1499 | + if found != false { |
| 1500 | + c.Errorf("unexpected grep results for %q, expected result to NOT contain: %v", tc.name, dontWantResult) |
| 1501 | + } |
| 1502 | + } |
| 1503 | + } |
| 1504 | +} |
0 commit comments