|
| 1 | +package docs |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "path/filepath" |
| 6 | + "testing" |
| 7 | +) |
| 8 | + |
| 9 | +func TestGetDocsPath(t *testing.T) { |
| 10 | + t.Parallel() |
| 11 | + |
| 12 | + t.Run("No path provided", func(t *testing.T) { |
| 13 | + t.Parallel() |
| 14 | + actual, err := GetDocsPath("") |
| 15 | + |
| 16 | + if err != nil { |
| 17 | + t.Errorf("GetDocsPath() error = %v, wantErr false", err) |
| 18 | + return |
| 19 | + } |
| 20 | + |
| 21 | + expected, _ := filepath.Abs(DefaultPath) |
| 22 | + if actual != expected { |
| 23 | + t.Errorf("GetDocsPath() = %v, want %v", actual, expected) |
| 24 | + } |
| 25 | + }) |
| 26 | + |
| 27 | + t.Run("With path provided", func(t *testing.T) { |
| 28 | + t.Parallel() |
| 29 | + inputPath := filepath.Join(os.TempDir(), "docs") |
| 30 | + actual, err := GetDocsPath(inputPath) |
| 31 | + |
| 32 | + if err != nil { |
| 33 | + t.Errorf("GetDocsPath() error = %v, wantErr false", err) |
| 34 | + return |
| 35 | + } |
| 36 | + |
| 37 | + expected, _ := filepath.Abs(inputPath) |
| 38 | + if actual != expected { |
| 39 | + t.Errorf("GetDocsPath() = %v, want %v", actual, expected) |
| 40 | + } |
| 41 | + |
| 42 | + if _, err := os.Stat(actual); os.IsNotExist(err) { |
| 43 | + t.Errorf("GetDocsPath() failed to create directory %s", actual) |
| 44 | + } |
| 45 | + }) |
| 46 | + |
| 47 | + t.Run("Invalid path", func(t *testing.T) { |
| 48 | + t.Parallel() |
| 49 | + invalidPath := string([]byte{0}) |
| 50 | + |
| 51 | + _, err := GetDocsPath(invalidPath) |
| 52 | + |
| 53 | + if err == nil { |
| 54 | + t.Errorf("GetDocsPath() error = nil, wantErr true") |
| 55 | + } |
| 56 | + }) |
| 57 | +} |
| 58 | + |
| 59 | +func TestGetDocsPath_ExistingDirectory(t *testing.T) { |
| 60 | + t.Parallel() |
| 61 | + |
| 62 | + tempDir, err := os.MkdirTemp("", "docs_test_existing") |
| 63 | + if err != nil { |
| 64 | + t.Fatalf("Failed to create temp dir: %v", err) |
| 65 | + } |
| 66 | + |
| 67 | + actual, err := GetDocsPath(tempDir) |
| 68 | + |
| 69 | + if err != nil { |
| 70 | + t.Errorf("GetDocsPath() error = %v, wantErr false", err) |
| 71 | + return |
| 72 | + } |
| 73 | + |
| 74 | + expected, _ := filepath.Abs(tempDir) |
| 75 | + if actual != expected { |
| 76 | + t.Errorf("GetDocsPath() = %v, want %v", actual, expected) |
| 77 | + } |
| 78 | + |
| 79 | + if _, err := os.Stat(actual); os.IsNotExist(err) { |
| 80 | + t.Errorf("GetDocsPath() failed to recognize existing directory %s", actual) |
| 81 | + } |
| 82 | +} |
0 commit comments