MCPcopy
hub / github.com/grafana/tempo / TestBackfillIssues

Function TestBackfillIssues

tools/chloggen/internal/chlog/entry_test.go:458–521  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

456}
457
458func TestBackfillIssues(t *testing.T) {
459 if _, err := exec.LookPath("git"); err != nil {
460 t.Skip("git not available")
461 }
462
463 dir := t.TempDir()
464 runGit := func(args ...string) {
465 t.Helper()
466 out, err := exec.Command("git", append([]string{"-C", dir}, args...)...).CombinedOutput()
467 require.NoError(t, err, string(out))
468 }
469 runGit("init")
470 runGit("config", "user.email", "test@example.com")
471 runGit("config", "user.name", "Test")
472
473 // Simulate Tempo's squash-merge: the commit that adds the entry file carries
474 // the PR number in its subject.
475 path := filepath.Join(dir, "my-change.yaml")
476 require.NoError(t, os.WriteFile(path, []byte("note: x\n"), 0o600))
477 runGit("add", "my-change.yaml")
478 runGit("commit", "-m", "Add a thing (#4242)")
479
480 t.Run("backfills PR from git when empty", func(t *testing.T) {
481 e := &Entry{path: path}
482 e.BackfillIssues()
483 assert.Equal(t, []int{4242}, e.Issues)
484 })
485
486 t.Run("keeps issues set by the author", func(t *testing.T) {
487 e := &Entry{path: path, Issues: []int{1}}
488 e.BackfillIssues()
489 assert.Equal(t, []int{1}, e.Issues)
490 })
491
492 t.Run("no-op when the file has no git history", func(t *testing.T) {
493 e := &Entry{path: filepath.Join(t.TempDir(), "untracked.yaml")}
494 e.BackfillIssues()
495 assert.Empty(t, e.Issues)
496 })
497
498 t.Run("uses the adding commit, not a later edit", func(t *testing.T) {
499 p := filepath.Join(dir, "edited.yaml")
500 require.NoError(t, os.WriteFile(p, []byte("note: a\n"), 0o600))
501 runGit("add", "edited.yaml")
502 runGit("commit", "-m", "Add edited (#100)")
503 require.NoError(t, os.WriteFile(p, []byte("note: b\n"), 0o600))
504 runGit("commit", "-am", "Tweak edited (#200)")
505
506 e := &Entry{path: p}
507 e.BackfillIssues()
508 assert.Equal(t, []int{100}, e.Issues)
509 })
510
511 t.Run("not found when the adding commit has no PR", func(t *testing.T) {
512 p := filepath.Join(dir, "nopr.yaml")
513 require.NoError(t, os.WriteFile(p, []byte("note: c\n"), 0o600))
514 runGit("add", "nopr.yaml")
515 runGit("commit", "-m", "local wip, no pr number")

Callers

nothing calls this directly

Calls 5

BackfillIssuesMethod · 0.95
SkipMethod · 0.80
JoinMethod · 0.65
RunMethod · 0.45
EqualMethod · 0.45

Tested by

no test coverage detected