-
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.
- Loading branch information
1 parent
a16ffb1
commit 736091a
Showing
4 changed files
with
208 additions
and
17 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
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,59 @@ | ||
package file | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
) | ||
|
||
type File struct { | ||
Path string | ||
} | ||
|
||
func (f File) IsSymlink() (bool, error) { | ||
fileInfo, err := os.Lstat(f.Path) | ||
if err != nil { | ||
return false, err | ||
} | ||
return fileInfo.Mode()&os.ModeSymlink != 0, nil | ||
} | ||
|
||
func (f File) FileType() string { | ||
// Get file info without following symlinks | ||
info, err := os.Lstat(f.Path) | ||
if err != nil { | ||
return "Error: Unable to get file info" | ||
} | ||
|
||
// Check if it's a symlink | ||
if info.Mode()&os.ModeSymlink != 0 { | ||
// If it's a symlink, we can optionally get the target | ||
target, err := os.Readlink(f.Path) | ||
if err != nil { | ||
return "Symlink (unable to read target)" | ||
} | ||
return fmt.Sprintf("Symlink to %s", target) | ||
} | ||
|
||
// If it's not a symlink, proceed with file type detection | ||
if info.IsDir() { | ||
return "Directory" | ||
} | ||
|
||
ext := strings.ToLower(filepath.Ext(f.Path)) | ||
switch ext { | ||
case ".txt": | ||
return "Text File" | ||
case ".go": | ||
return "Go Source File" | ||
case ".jpg", ".jpeg": | ||
return "JPEG Image" | ||
case ".png": | ||
return "PNG Image" | ||
case ".pdf": | ||
return "PDF Document" | ||
default: | ||
return "Unknown File Type" | ||
} | ||
} |
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,104 @@ | ||
package file | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func TestFileType(t *testing.T) { | ||
// Create a temporary directory for our test files | ||
tmpDir, err := os.MkdirTemp("", "filetest") | ||
if err != nil { | ||
t.Fatalf("Failed to create temp dir: %v", err) | ||
} | ||
defer os.RemoveAll(tmpDir) | ||
|
||
// Test cases | ||
tests := []struct { | ||
name string | ||
setup func() (string, error) | ||
expected string | ||
}{ | ||
{ | ||
name: "Text File", | ||
setup: func() (string, error) { | ||
path := filepath.Join(tmpDir, "test.txt") | ||
err := os.WriteFile(path, []byte("test"), 0o644) | ||
return path, err | ||
}, | ||
expected: "Text File", | ||
}, | ||
{ | ||
name: "Go Source File", | ||
setup: func() (string, error) { | ||
path := filepath.Join(tmpDir, "test.go") | ||
err := os.WriteFile(path, []byte("package main"), 0o644) | ||
return path, err | ||
}, | ||
expected: "Go Source File", | ||
}, | ||
{ | ||
name: "JPEG Image", | ||
setup: func() (string, error) { | ||
path := filepath.Join(tmpDir, "test.jpg") | ||
err := os.WriteFile(path, []byte("fake jpg"), 0o644) | ||
return path, err | ||
}, | ||
expected: "JPEG Image", | ||
}, | ||
{ | ||
name: "Directory", | ||
setup: func() (string, error) { | ||
path := filepath.Join(tmpDir, "testdir") | ||
err := os.Mkdir(path, 0o755) | ||
return path, err | ||
}, | ||
expected: "Directory", | ||
}, | ||
{ | ||
name: "Symlink", | ||
setup: func() (string, error) { | ||
target := filepath.Join(tmpDir, "target.txt") | ||
err := os.WriteFile(target, []byte("target"), 0o644) | ||
if err != nil { | ||
return "", err | ||
} | ||
link := filepath.Join(tmpDir, "symlink") | ||
err = os.Symlink(target, link) | ||
return link, err | ||
}, | ||
expected: "Symlink to ", // We'll check if it starts with this | ||
}, | ||
{ | ||
name: "Unknown File Type", | ||
setup: func() (string, error) { | ||
path := filepath.Join(tmpDir, "test.xyz") | ||
err := os.WriteFile(path, []byte("unknown"), 0o644) | ||
return path, err | ||
}, | ||
expected: "Unknown File Type", | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
path, err := tt.setup() | ||
if err != nil { | ||
t.Fatalf("Setup failed: %v", err) | ||
} | ||
|
||
file := File{Path: path} | ||
result := file.FileType() | ||
|
||
if tt.name == "Symlink" { | ||
if !strings.HasPrefix(result, tt.expected) { | ||
t.Errorf("FileType() = %v, want prefix %v", result, tt.expected) | ||
} | ||
} else if result != tt.expected { | ||
t.Errorf("FileType() = %v, want %v", result, tt.expected) | ||
} | ||
}) | ||
} | ||
} |
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