@@ -3,6 +3,7 @@ package internal
33import (
44 "bytes"
55 "log"
6+ "os/exec"
67 "strings"
78 "testing"
89)
@@ -12,15 +13,36 @@ func boolPtr(b bool) *bool {
1213 return & b
1314}
1415
16+ var capturedArgs []string
17+
18+ var mockExecCommand = func (name string , args ... string ) * exec.Cmd {
19+ capturedArgs = args // Capture arguments for assertions
20+ if name == "rsync" {
21+ if strings .Contains (strings .Join (args , " " ), "/invalid/source/path" ) {
22+ cmd := exec .Command ("false" ) // Simulate failure for invalid paths
23+ cmd .Args = append ([]string {name }, args ... )
24+ return cmd
25+ }
26+ cmd := exec .Command ("echo" )
27+ cmd .Args = append ([]string {name }, args ... )
28+ return cmd
29+ }
30+ return exec .Command (name , args ... )
31+ }
32+
33+ func init () {
34+ execCommand = mockExecCommand
35+ }
36+
1537func TestBuildRsyncCmd (t * testing.T ) {
1638 job := Job {
1739 Source : "/home/user/Music/" ,
1840 Target : "/target/user/music/home" ,
1941 Delete : nil ,
2042 Exclusions : []string {"*.tmp" , "node_modules/" },
2143 }
22- dryRun := true
23- cmd := buildRsyncCmd (job , dryRun )
44+ simulate := true
45+ cmd := buildRsyncCmd (job , simulate )
2446
2547 expectedArgs := []string {
2648 "rsync" , "--dry-run" , "-aiv" , "--info=progress2" , "--delete" ,
@@ -41,10 +63,10 @@ func TestExecuteJob(t *testing.T) {
4163 Delete : nil ,
4264 Exclusions : []string {"*.tmp" },
4365 }
44- dryRun := true
66+ simulate := true
4567 logger := log .New (& bytes.Buffer {}, "" , log .LstdFlags )
4668
47- status := ExecuteJob (job , dryRun , logger )
69+ status := ExecuteJob (job , simulate , logger )
4870 if status != "SUCCESS" {
4971 t .Errorf ("Expected status SUCCESS, got %s" , status )
5072 }
@@ -56,7 +78,7 @@ func TestExecuteJob(t *testing.T) {
5678 Enabled : boolPtr (false ),
5779 }
5880
59- status = ExecuteJob (disabledJob , dryRun , logger )
81+ status = ExecuteJob (disabledJob , simulate , logger )
6082 if status != "SKIPPED" {
6183 t .Errorf ("Expected status SKIPPED, got %s" , status )
6284 }
@@ -81,10 +103,10 @@ func TestJobSkippedEnabledTrue(t *testing.T) {
81103 Target : "/mnt/backup1/test/" ,
82104 Enabled : boolPtr (true ),
83105 }
84- dryRun := true
106+ simulate := true
85107 logger := log .New (& bytes.Buffer {}, "" , log .LstdFlags )
86108
87- status := ExecuteJob (job , dryRun , logger )
109+ status := ExecuteJob (job , simulate , logger )
88110 if status != "SUCCESS" {
89111 t .Errorf ("Expected status SUCCESS, got %s" , status )
90112 }
@@ -97,10 +119,10 @@ func TestJobSkippedEnabledFalse(t *testing.T) {
97119 Target : "/mnt/backup1/disabled/" ,
98120 Enabled : boolPtr (false ),
99121 }
100- dryRun := true
122+ simulate := true
101123 logger := log .New (& bytes.Buffer {}, "" , log .LstdFlags )
102124
103- status := ExecuteJob (disabledJob , dryRun , logger )
125+ status := ExecuteJob (disabledJob , simulate , logger )
104126 if status != "SKIPPED" {
105127 t .Errorf ("Expected status SKIPPED, got %s" , status )
106128 }
@@ -112,11 +134,32 @@ func TestJobSkippedEnabledOmitted(t *testing.T) {
112134 Source : "/home/omitted/" ,
113135 Target : "/mnt/backup1/omitted/" ,
114136 }
115- dryRun := true
137+ simulate := true
116138 logger := log .New (& bytes.Buffer {}, "" , log .LstdFlags )
117139
118- status := ExecuteJob (job , dryRun , logger )
140+ status := ExecuteJob (job , simulate , logger )
119141 if status != "SUCCESS" {
120142 t .Errorf ("Expected status SUCCESS, got %s" , status )
121143 }
122144}
145+
146+ func TestExecuteJobWithMockedRsync (t * testing.T ) {
147+ // Reset capturedArgs before the test
148+ capturedArgs = nil
149+
150+ job := Job {
151+ Name : "test_job" ,
152+ Source : "/home/test/" ,
153+ Target : "/mnt/backup1/test/" ,
154+ Delete : nil ,
155+ Exclusions : []string {"*.tmp" },
156+ }
157+ simulate := true
158+ logger := log .New (& bytes.Buffer {}, "" , log .LstdFlags )
159+
160+ _ = ExecuteJob (job , simulate , logger )
161+
162+ if len (capturedArgs ) == 0 || capturedArgs [0 ] != "--dry-run" {
163+ t .Errorf ("Expected --dry-run flag, got %v" , capturedArgs )
164+ }
165+ }
0 commit comments