(t *testing.T)
| 69 | } |
| 70 | |
| 71 | func TestRegisterAndUnregisterRunCommand_Bash(t *testing.T) { |
| 72 | home := t.TempDir() |
| 73 | b := NewBashCompletion(home, &cobra.Command{Use: "celer"}) |
| 74 | |
| 75 | // prepare a .bashrc without the register line |
| 76 | bashrc := filepath.Join(home, ".bashrc") |
| 77 | if err := os.WriteFile(bashrc, []byte("export LANG=C\n"), 0644); err != nil { |
| 78 | t.Fatalf("failed to write .bashrc: %v", err) |
| 79 | } |
| 80 | |
| 81 | // Register should append the registerBinary line |
| 82 | if err := b.registerRunCommand(); err != nil { |
| 83 | t.Fatalf("registerRunCommand failed: %v", err) |
| 84 | } |
| 85 | data, err := os.ReadFile(bashrc) |
| 86 | if err != nil { |
| 87 | t.Fatalf("failed to read .bashrc: %v", err) |
| 88 | } |
| 89 | if !strings.Contains(string(data), b.registerBinary) { |
| 90 | t.Fatalf("expected .bashrc to contain register line, got: %s", string(data)) |
| 91 | } |
| 92 | |
| 93 | // Calling register again should not duplicate the line |
| 94 | if err := b.registerRunCommand(); err != nil { |
| 95 | t.Fatalf("second registerRunCommand failed: %v", err) |
| 96 | } |
| 97 | data, _ = os.ReadFile(bashrc) |
| 98 | if count := strings.Count(string(data), b.registerBinary); count != 1 { |
| 99 | t.Fatalf("expected register line to appear once, got %d", count) |
| 100 | } |
| 101 | |
| 102 | // Now test unregister: create a .bashrc that has the register line plus other lines |
| 103 | if err := os.WriteFile(bashrc, []byte("line1\n"+b.registerBinary+"\nline2\n"), 0644); err != nil { |
| 104 | t.Fatalf("failed to write .bashrc for unregister test: %v", err) |
| 105 | } |
| 106 | if err := b.unregisterRunCommand(); err != nil { |
| 107 | t.Fatalf("unregisterRunCommand failed: %v", err) |
| 108 | } |
| 109 | data, _ = os.ReadFile(bashrc) |
| 110 | if strings.Contains(string(data), b.registerBinary) { |
| 111 | t.Fatalf("expected register line to be removed, still present in: %s", string(data)) |
| 112 | } |
| 113 | if !strings.Contains(string(data), "line1") || !strings.Contains(string(data), "line2") { |
| 114 | t.Fatalf("expected other lines to be preserved, got: %s", string(data)) |
| 115 | } |
| 116 | } |
nothing calls this directly
no test coverage detected