Skip to content

Commit cf87fec

Browse files
authored
feat: improve file handling with OS-specific functions and tests (#178)
- Add functions for removing and creating files/directories based on the operating system - Add unit tests for the functions
1 parent 665c665 commit cf87fec

2 files changed

Lines changed: 50 additions & 1 deletion

File tree

command.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,29 @@
11
package main
22

3+
// This function returns the appropriate command for removing a file/directory based on the operating system.
34
func rmcmd(os, target string) string {
45
switch os {
56
case "windows":
7+
// On Windows, use DEL command to delete files and folders recursively
68
return "DEL /F /S " + target
79
case "unix":
10+
// On Unix-based systems, use rm command to delete files and folders recursively
811
return "rm -rf " + target
912
}
13+
// Return an empty string if the operating system is not recognized
1014
return ""
1115
}
1216

17+
// This function returns the appropriate command for creating a directory based on the operating system.
1318
func mkdircmd(os, target string) string {
1419
switch os {
1520
case "windows":
21+
// On Windows, use mkdir command to create directory and check if it exists
1622
return "if not exist " + target + " mkdir " + target
1723
case "unix":
24+
// On Unix-based systems, use mkdir command with -p option to create directories recursively
1825
return "mkdir -p " + target
1926
}
20-
27+
// Return an empty string if the operating system is not recognized
2128
return ""
2229
}

command_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package main
2+
3+
import "testing"
4+
5+
// Unit tests for rmcmd and mkdircmd
6+
func TestCommands(t *testing.T) {
7+
// Test rmcmd on Windows
8+
os1 := "windows"
9+
target1 := "C:\\path\\to\\file"
10+
expected1 := "DEL /F /S " + target1
11+
actual1 := rmcmd(os1, target1)
12+
if actual1 != expected1 {
13+
t.Errorf("rmcmd(%s, %s) = %s; expected %s", os1, target1, actual1, expected1)
14+
}
15+
16+
// Test rmcmd on Unix-based system
17+
os2 := "unix"
18+
target2 := "/path/to/folder"
19+
expected2 := "rm -rf " + target2
20+
actual2 := rmcmd(os2, target2)
21+
if actual2 != expected2 {
22+
t.Errorf("rmcmd(%s, %s) = %s; expected %s", os2, target2, actual2, expected2)
23+
}
24+
25+
// Test mkdircmd on Windows
26+
os3 := "windows"
27+
target3 := "C:\\path\\to\\folder"
28+
expected3 := "if not exist " + target3 + " mkdir " + target3
29+
actual3 := mkdircmd(os3, target3)
30+
if actual3 != expected3 {
31+
t.Errorf("mkdircmd(%s, %s) = %s; expected %s", os3, target3, actual3, expected3)
32+
}
33+
34+
// Test mkdircmd on Unix-based system
35+
os4 := "unix"
36+
target4 := "/path/to/folder"
37+
expected4 := "mkdir -p " + target4
38+
actual4 := mkdircmd(os4, target4)
39+
if actual4 != expected4 {
40+
t.Errorf("mkdircmd(%s, %s) = %s; expected %s", os4, target4, actual4, expected4)
41+
}
42+
}

0 commit comments

Comments
 (0)