root_test.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. package cmd
  2. import (
  3. "bytes"
  4. "io"
  5. "os"
  6. "testing"
  7. )
  8. func TestCheckStdinPipe(t *testing.T) {
  9. // Save original stdin
  10. origStdin := os.Stdin
  11. // Restore original stdin when test completes
  12. defer func() {
  13. os.Stdin = origStdin
  14. }()
  15. // Test case 1: Data is piped in
  16. t.Run("WithPipedData", func(t *testing.T) {
  17. // Create a pipe
  18. r, w, err := os.Pipe()
  19. if err != nil {
  20. t.Fatalf("Failed to create pipe: %v", err)
  21. }
  22. // Replace stdin with our pipe
  23. os.Stdin = r
  24. // Write test data to the pipe
  25. testData := "test piped input"
  26. go func() {
  27. defer w.Close()
  28. w.Write([]byte(testData))
  29. }()
  30. // Call the function
  31. data, hasPiped := checkStdinPipe()
  32. // Check results
  33. if !hasPiped {
  34. t.Error("Expected hasPiped to be true, got false")
  35. }
  36. if data != testData {
  37. t.Errorf("Expected data to be %q, got %q", testData, data)
  38. }
  39. })
  40. // Test case 2: No data is piped in (simulated terminal)
  41. t.Run("WithoutPipedData", func(t *testing.T) {
  42. // Create a temporary file to simulate a terminal
  43. tmpFile, err := os.CreateTemp("", "terminal-sim")
  44. if err != nil {
  45. t.Fatalf("Failed to create temp file: %v", err)
  46. }
  47. defer os.Remove(tmpFile.Name())
  48. defer tmpFile.Close()
  49. // Open the file for reading
  50. f, err := os.Open(tmpFile.Name())
  51. if err != nil {
  52. t.Fatalf("Failed to open temp file: %v", err)
  53. }
  54. defer f.Close()
  55. // Replace stdin with our file
  56. os.Stdin = f
  57. // Call the function
  58. data, hasPiped := checkStdinPipe()
  59. // Check results
  60. if hasPiped {
  61. t.Error("Expected hasPiped to be false, got true")
  62. }
  63. if data != "" {
  64. t.Errorf("Expected data to be empty, got %q", data)
  65. }
  66. })
  67. }
  68. // This is a mock implementation for testing since we can't easily mock os.Stdin.Stat()
  69. // in a way that would return the correct Mode() for our test cases
  70. func mockCheckStdinPipe(reader io.Reader, isPipe bool) (string, bool) {
  71. if !isPipe {
  72. return "", false
  73. }
  74. data, err := io.ReadAll(reader)
  75. if err != nil {
  76. return "", false
  77. }
  78. if len(data) > 0 {
  79. return string(data), true
  80. }
  81. return "", false
  82. }
  83. func TestMockCheckStdinPipe(t *testing.T) {
  84. // Test with data
  85. t.Run("WithData", func(t *testing.T) {
  86. testData := "test data"
  87. reader := bytes.NewBufferString(testData)
  88. data, hasPiped := mockCheckStdinPipe(reader, true)
  89. if !hasPiped {
  90. t.Error("Expected hasPiped to be true, got false")
  91. }
  92. if data != testData {
  93. t.Errorf("Expected data to be %q, got %q", testData, data)
  94. }
  95. })
  96. // Test without data
  97. t.Run("WithoutData", func(t *testing.T) {
  98. reader := bytes.NewBufferString("")
  99. data, hasPiped := mockCheckStdinPipe(reader, true)
  100. if hasPiped {
  101. t.Error("Expected hasPiped to be false, got true")
  102. }
  103. if data != "" {
  104. t.Errorf("Expected data to be empty, got %q", data)
  105. }
  106. })
  107. // Test not a pipe
  108. t.Run("NotAPipe", func(t *testing.T) {
  109. reader := bytes.NewBufferString("data that should be ignored")
  110. data, hasPiped := mockCheckStdinPipe(reader, false)
  111. if hasPiped {
  112. t.Error("Expected hasPiped to be false, got true")
  113. }
  114. if data != "" {
  115. t.Errorf("Expected data to be empty, got %q", data)
  116. }
  117. })
  118. }