@@ -680,3 +680,68 @@ func Test_execCmdWithTmp(t *testing.T) {
680
680
assert .Contains (t , wr .String (), fmt .Sprintf ("cannot access '%s'" , tmpPath ))
681
681
})
682
682
}
683
+
684
+ func Test_execCmd_prepScript (t * testing.T ) {
685
+ testingHostAndPort , teardown := startTestContainer (t )
686
+ defer teardown ()
687
+
688
+ ctx := context .Background ()
689
+ logs := executor .MakeLogs (false , false , nil )
690
+ connector , connErr := executor .NewConnector ("testdata/test_ssh_key" , time .Second * 10 , logs )
691
+ require .NoError (t , connErr )
692
+ sess , errSess := connector .Connect (ctx , testingHostAndPort , "my-host" , "test" )
693
+ require .NoError (t , errSess )
694
+
695
+ t .Run ("single line command, no temp files" , func (t * testing.T ) {
696
+ ec := execCmd {exec : sess , tsk : & config.Task {Name : "test" }}
697
+ cmd , script , teardown , err := ec .prepScript (ctx , "echo hello" , nil )
698
+ require .NoError (t , err )
699
+ assert .Equal (t , "echo hello" , cmd )
700
+ assert .Empty (t , script )
701
+ assert .Nil (t , teardown )
702
+ })
703
+
704
+ t .Run ("multiline script with temp files" , func (t * testing.T ) {
705
+ input := "#!/bin/sh\n echo 'line 1'\n echo 'line 2'\n "
706
+ ec := execCmd {exec : sess , tsk : & config.Task {Name : "test" }}
707
+
708
+ // capture log output
709
+ var buf bytes.Buffer
710
+ log .SetOutput (& buf )
711
+ defer log .SetOutput (os .Stdout )
712
+
713
+ cmd , _ , teardown , err := ec .prepScript (ctx , "" , bytes .NewBufferString (input ))
714
+ require .NoError (t , err )
715
+ require .NotNil (t , teardown )
716
+ defer teardown ()
717
+
718
+ // verify command format
719
+ assert .Contains (t , cmd , "/bin/sh -c /tmp/.spot-" )
720
+
721
+ // verify script content matches input
722
+ remotePath := strings .Split (cmd , " " )[2 ]
723
+ out , err := sess .Run (ctx , fmt .Sprintf ("cat %s" , remotePath ), nil )
724
+ require .NoError (t , err )
725
+ assert .Equal (t , input , strings .Join (out , "\n " )+ "\n " )
726
+
727
+ // verify local temp file was removed (check logs)
728
+ assert .Contains (t , buf .String (), "[DEBUG] removed local temp script" )
729
+ })
730
+
731
+ t .Run ("failed upload cleanup" , func (t * testing.T ) {
732
+ // create invalid executor that will fail on upload
733
+ invalidSess := & executor.Remote {} // This will fail on upload
734
+
735
+ // capture log output
736
+ var buf bytes.Buffer
737
+ log .SetOutput (& buf )
738
+ defer log .SetOutput (os .Stdout )
739
+
740
+ ec := execCmd {exec : invalidSess , tsk : & config.Task {Name : "test" }}
741
+ _ , _ , _ , err := ec .prepScript (ctx , "" , bytes .NewBufferString ("echo test" ))
742
+ require .Error (t , err )
743
+
744
+ // verify local temp file was removed even on error (check logs)
745
+ assert .Contains (t , buf .String (), "[DEBUG] removed local temp script" )
746
+ })
747
+ }
0 commit comments