-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: cleanup code, add tests for fileops (#13)
- Loading branch information
Showing
8 changed files
with
242 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
name: ⚙️ CI | ||
|
||
on: | ||
push: | ||
branches: [main] | ||
pull_request: | ||
branches: [main] | ||
|
||
jobs: | ||
validate: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v4 | ||
|
||
- name: Set up Go | ||
uses: actions/setup-go@v5 | ||
with: | ||
go-version: stable | ||
|
||
- name: Verify dependencies | ||
run: go mod verify | ||
|
||
- name: Run go vet | ||
run: go vet ./... | ||
|
||
- name: Run tests | ||
run: go test -v -cover ./... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package fileops | ||
|
||
import ( | ||
"os" | ||
) | ||
|
||
// CreateDir creates a directory and all necessary parent directories. | ||
// It returns an error if the operation fails. | ||
func CreateDir(path string) error { | ||
return os.MkdirAll(path, os.ModePerm) | ||
} | ||
|
||
// CreateFile creates a new file with the given content. | ||
// It returns an error if the file cannot be created or written to. | ||
func CreateFile(path, content string) error { | ||
return os.WriteFile(path, []byte(content), 0644) | ||
} | ||
|
||
// PathExists checks if a file or directory exists. | ||
// It returns true if the path exists, false otherwise. | ||
func PathExists(path string) bool { | ||
_, err := os.Stat(path) | ||
return !os.IsNotExist(err) | ||
} | ||
|
||
// AppendToFile appends content to an existing file or creates a new one if it doesn't exist. | ||
// It returns an error if the file cannot be opened, created, or written to. | ||
func AppendToFile(path, content string) error { | ||
file, err := os.OpenFile(path, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) | ||
if err != nil { | ||
return err | ||
} | ||
defer file.Close() | ||
_, err = file.WriteString(content) | ||
return err | ||
} | ||
|
||
// CreateDirectoryIfNotFound creates a directory if it doesn't exist. | ||
// It returns true if the directory was created, false if it already existed. | ||
// If an error occurs during creation, it returns false and the error. | ||
func CreateDirectoryIfNotFound(path string) (bool, error) { | ||
if !PathExists(path) { | ||
err := CreateDir(path) | ||
if err != nil { | ||
return false, err | ||
} | ||
return true, nil | ||
} | ||
return false, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
package fileops | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
) | ||
|
||
func TestCreateDir(t *testing.T) { | ||
testDir := filepath.Join(os.TempDir(), "test_create_dir") | ||
defer os.RemoveAll(testDir) | ||
|
||
err := CreateDir(testDir) | ||
if err != nil { | ||
t.Fatalf("CreateDir failed: %v", err) | ||
} | ||
|
||
if !PathExists(testDir) { | ||
t.Errorf("Directory was not created") | ||
} | ||
} | ||
|
||
func TestCreateFile(t *testing.T) { | ||
testFile := filepath.Join(os.TempDir(), "test_create_file.txt") | ||
defer os.Remove(testFile) | ||
|
||
content := "Test content" | ||
err := CreateFile(testFile, content) | ||
if err != nil { | ||
t.Fatalf("CreateFile failed: %v", err) | ||
} | ||
|
||
if !PathExists(testFile) { | ||
t.Errorf("File was not created") | ||
} | ||
|
||
data, err := os.ReadFile(testFile) | ||
if err != nil { | ||
t.Fatalf("Failed to read file: %v", err) | ||
} | ||
|
||
if string(data) != content { | ||
t.Errorf("File content does not match. Expected: %s, Got: %s", content, string(data)) | ||
} | ||
} | ||
|
||
func TestCreateFileInNonExistentDir(t *testing.T) { | ||
nonExistentDir := filepath.Join(os.TempDir(), "non_existent_dir") | ||
nonExistentFile := filepath.Join(nonExistentDir, "test.txt") | ||
defer os.RemoveAll(nonExistentDir) | ||
|
||
err := CreateFile(nonExistentFile, "content") | ||
if err == nil { | ||
t.Errorf("CreateFile should fail when creating file in non-existent directory") | ||
} | ||
} | ||
|
||
func TestPathExists(t *testing.T) { | ||
existingFile := filepath.Join(os.TempDir(), "existing_file.txt") | ||
err := os.WriteFile(existingFile, []byte("test"), 0644) | ||
if err != nil { | ||
t.Fatalf("Failed to create test file: %v", err) | ||
} | ||
defer os.Remove(existingFile) | ||
|
||
if !PathExists(existingFile) { | ||
t.Errorf("PathExists returned false for an existing file") | ||
} | ||
} | ||
|
||
func TestPathDoesNotExist(t *testing.T) { | ||
nonExistingFile := filepath.Join(os.TempDir(), "non_existing_file.txt") | ||
|
||
if PathExists(nonExistingFile) { | ||
t.Errorf("PathExists returned true for a non-existing file") | ||
} | ||
} | ||
|
||
func TestAppendToFile(t *testing.T) { | ||
testFile := filepath.Join(os.TempDir(), "test_append_file.txt") | ||
defer os.Remove(testFile) | ||
|
||
initialContent := "Initial content\n" | ||
appendedContent := "Appended content" | ||
|
||
err := CreateFile(testFile, initialContent) | ||
if err != nil { | ||
t.Fatalf("Failed to create initial file: %v", err) | ||
} | ||
|
||
err = AppendToFile(testFile, appendedContent) | ||
if err != nil { | ||
t.Fatalf("AppendToFile failed: %v", err) | ||
} | ||
|
||
data, err := os.ReadFile(testFile) | ||
if err != nil { | ||
t.Fatalf("Failed to read file: %v", err) | ||
} | ||
|
||
expectedContent := initialContent + appendedContent | ||
if string(data) != expectedContent { | ||
t.Errorf("File content does not match. Expected: %s, Got: %s", expectedContent, string(data)) | ||
} | ||
} | ||
|
||
func TestAppendToReadOnlyFile(t *testing.T) { | ||
readOnlyFile := filepath.Join(os.TempDir(), "readonly_file.txt") | ||
err := os.WriteFile(readOnlyFile, []byte("Read-only content"), 0444) | ||
if err != nil { | ||
t.Fatalf("Failed to create read-only file: %v", err) | ||
} | ||
defer os.Remove(readOnlyFile) | ||
|
||
err = AppendToFile(readOnlyFile, "Appended content") | ||
if err == nil { | ||
t.Errorf("AppendToFile should fail when appending to a read-only file") | ||
} | ||
} | ||
|
||
func TestCreateDirectoryIfNotFound(t *testing.T) { | ||
testDir := filepath.Join(os.TempDir(), "test_create_dir_if_not_found") | ||
defer os.RemoveAll(testDir) | ||
|
||
created, err := CreateDirectoryIfNotFound(testDir) | ||
if err != nil { | ||
t.Fatalf("CreateDirectoryIfNotFound failed: %v", err) | ||
} | ||
if !created { | ||
t.Errorf("CreateDirectoryIfNotFound returned false for a new directory") | ||
} | ||
if !PathExists(testDir) { | ||
t.Errorf("Directory was not created") | ||
} | ||
} | ||
|
||
func TestCreateExistingDirectory(t *testing.T) { | ||
testDir := filepath.Join(os.TempDir(), "test_create_existing_dir") | ||
err := os.Mkdir(testDir, 0755) | ||
if err != nil { | ||
t.Fatalf("Failed to create test directory: %v", err) | ||
} | ||
defer os.RemoveAll(testDir) | ||
|
||
created, err := CreateDirectoryIfNotFound(testDir) | ||
if err != nil { | ||
t.Fatalf("CreateDirectoryIfNotFound failed for existing directory: %v", err) | ||
} | ||
if created { | ||
t.Errorf("CreateDirectoryIfNotFound returned true for an existing directory") | ||
} | ||
} |