status_test.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. package status
  2. import (
  3. "os"
  4. "path/filepath"
  5. "testing"
  6. "time"
  7. )
  8. func TestGetCurrentGitBranch(t *testing.T) {
  9. // Test in current directory (should be a git repo)
  10. branch := getCurrentGitBranch(".")
  11. if branch == "" {
  12. t.Skip("Not in a git repository, skipping test")
  13. }
  14. t.Logf("Current branch: %s", branch)
  15. }
  16. func TestGetGitRefFile(t *testing.T) {
  17. // Create a temporary git directory structure for testing
  18. tmpDir := t.TempDir()
  19. gitDir := filepath.Join(tmpDir, ".git")
  20. err := os.MkdirAll(gitDir, 0755)
  21. if err != nil {
  22. t.Fatal(err)
  23. }
  24. // Test case 1: HEAD points to a ref
  25. headFile := filepath.Join(gitDir, "HEAD")
  26. err = os.WriteFile(headFile, []byte("ref: refs/heads/main\n"), 0644)
  27. if err != nil {
  28. t.Fatal(err)
  29. }
  30. refFile := getGitRefFile(tmpDir)
  31. expected := filepath.Join(gitDir, "refs", "heads", "main")
  32. if refFile != expected {
  33. t.Errorf("Expected %s, got %s", expected, refFile)
  34. }
  35. // Test case 2: HEAD contains a direct commit hash
  36. err = os.WriteFile(headFile, []byte("abc123def456\n"), 0644)
  37. if err != nil {
  38. t.Fatal(err)
  39. }
  40. refFile = getGitRefFile(tmpDir)
  41. if refFile != headFile {
  42. t.Errorf("Expected %s, got %s", headFile, refFile)
  43. }
  44. }
  45. func TestFileWatcherIntegration(t *testing.T) {
  46. // This test requires being in a git repository
  47. if getCurrentGitBranch(".") == "" {
  48. t.Skip("Not in a git repository, skipping integration test")
  49. }
  50. // Test that the file watcher setup doesn't crash
  51. tmpDir := t.TempDir()
  52. gitDir := filepath.Join(tmpDir, ".git")
  53. err := os.MkdirAll(gitDir, 0755)
  54. if err != nil {
  55. t.Fatal(err)
  56. }
  57. headFile := filepath.Join(gitDir, "HEAD")
  58. err = os.WriteFile(headFile, []byte("ref: refs/heads/main\n"), 0644)
  59. if err != nil {
  60. t.Fatal(err)
  61. }
  62. // Create the refs directory and file
  63. refsDir := filepath.Join(gitDir, "refs", "heads")
  64. err = os.MkdirAll(refsDir, 0755)
  65. if err != nil {
  66. t.Fatal(err)
  67. }
  68. mainRef := filepath.Join(refsDir, "main")
  69. err = os.WriteFile(mainRef, []byte("abc123def456\n"), 0644)
  70. if err != nil {
  71. t.Fatal(err)
  72. }
  73. // Test that we can create a watcher without crashing
  74. // This is a basic smoke test
  75. done := make(chan bool, 1)
  76. go func() {
  77. time.Sleep(100 * time.Millisecond)
  78. done <- true
  79. }()
  80. select {
  81. case <-done:
  82. // Test passed - no crash
  83. case <-time.After(1 * time.Second):
  84. t.Error("Test timed out")
  85. }
  86. }