| 79 | } |
| 80 | |
| 81 | func TestReadFile(t *testing.T) { |
| 82 | tests := []struct { |
| 83 | name string |
| 84 | content string |
| 85 | expectError bool |
| 86 | }{ |
| 87 | { |
| 88 | name: "read simple file", |
| 89 | content: "hello world", |
| 90 | expectError: false, |
| 91 | }, |
| 92 | { |
| 93 | name: "read empty file", |
| 94 | content: "", |
| 95 | expectError: false, |
| 96 | }, |
| 97 | { |
| 98 | name: "read file with special characters", |
| 99 | content: "hello\nworld\t!@#$%^&*()", |
| 100 | expectError: false, |
| 101 | }, |
| 102 | } |
| 103 | |
| 104 | for _, tt := range tests { |
| 105 | t.Run(tt.name, func(t *testing.T) { |
| 106 | tempDir := t.TempDir() |
| 107 | filename := filepath.Join(tempDir, "test.txt") |
| 108 | |
| 109 | err := os.WriteFile(filename, []byte(tt.content), 0644) |
| 110 | assert.NoError(t, err, "Failed to create test file") |
| 111 | |
| 112 | content, err := readFile(filename) |
| 113 | |
| 114 | if tt.expectError { |
| 115 | assert.Error(t, err, "readFile() should return an error") |
| 116 | return |
| 117 | } |
| 118 | |
| 119 | assert.NoError(t, err, "readFile() should not return an error") |
| 120 | assert.Equal(t, tt.content, content, "readFile() content mismatch") |
| 121 | }) |
| 122 | } |
| 123 | |
| 124 | t.Run("read nonexistent file", func(t *testing.T) { |
| 125 | _, err := readFile("/nonexistent/file.txt") |
| 126 | assert.Error(t, err, "readFile() should return an error for nonexistent file") |
| 127 | }) |
| 128 | } |
| 129 | |
| 130 | func TestSanitizePackageName(t *testing.T) { |
| 131 | tests := []struct { |