MCPcopy Create free account
hub / github.com/github/gh-aw / TestEnsureGitAttributes

Function TestEnsureGitAttributes

pkg/cli/gitattributes_test.go:12–142  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

10)
11
12func TestEnsureGitAttributes(t *testing.T) {
13 // Create a temporary directory for testing
14 tmpDir, err := os.MkdirTemp("", "gh-aw-gitattributes-test")
15 if err != nil {
16 t.Fatalf("Failed to create temp dir: %v", err)
17 }
18 defer os.RemoveAll(tmpDir)
19
20 // Change to temp directory
21 originalDir, err := os.Getwd()
22 if err != nil {
23 t.Fatalf("Failed to get current directory: %v", err)
24 }
25 defer func() {
26 _ = os.Chdir(originalDir)
27 }()
28
29 if err := os.Chdir(tmpDir); err != nil {
30 t.Fatalf("Failed to change to temp directory: %v", err)
31 }
32
33 // Initialize a git repository
34 if err := os.WriteFile("test.txt", []byte("test"), 0644); err != nil {
35 t.Fatalf("Failed to create test file: %v", err)
36 }
37 if cmd := exec.Command("git", "init"); cmd.Run() != nil {
38 t.Skip("Skipping test - git not available")
39 }
40
41 tests := []struct {
42 name string
43 existingContent string
44 expectedContent string
45 expectUpdated bool
46 }{
47 {
48 name: "creates new gitattributes file",
49 existingContent: "",
50 expectedContent: ".github/workflows/*.lock.yml linguist-generated=true merge=ours",
51 expectUpdated: true,
52 },
53 {
54 name: "adds entry to existing file",
55 existingContent: "*.generated linguist-generated=true\n",
56 expectedContent: "*.generated linguist-generated=true\n\n.github/workflows/*.lock.yml linguist-generated=true merge=ours",
57 expectUpdated: true,
58 },
59 {
60 name: "does not duplicate existing entry",
61 existingContent: ".github/workflows/*.lock.yml linguist-generated=true merge=ours\n",
62 expectedContent: ".github/workflows/*.lock.yml linguist-generated=true merge=ours",
63 expectUpdated: false,
64 },
65 {
66 name: "does not duplicate entry with different order",
67 existingContent: "*.md linguist-documentation=true\n.github/workflows/*.lock.yml linguist-generated=true merge=ours\n*.txt text=auto\n",
68 expectedContent: "*.md linguist-documentation=true\n.github/workflows/*.lock.yml linguist-generated=true merge=ours\n*.txt text=auto",
69 expectUpdated: false,

Callers

nothing calls this directly

Calls 3

ensureGitAttributesFunction · 0.85
RunMethod · 0.45
ErrorfMethod · 0.45

Tested by

no test coverage detected