@@ -808,3 +808,209 @@ Deno.test("getPullRequestCommentTemplate - should handle empty file", async () =
808808 Deno . env . delete ( "INPUT_PULL_REQUEST_COMMENT_TEMPLATE_FILE" )
809809 }
810810} )
811+
812+ // Tests for getUserScriptCurrentWorkingDirectory()
813+
814+ Deno . test ( "getUserScriptCurrentWorkingDirectory - should return git directory when input is not set" , ( ) => {
815+ Deno . env . delete ( "INPUT_CURRENT_WORKING_DIRECTORY" )
816+
817+ const gitDirectory = Deno . cwd ( )
818+
819+ const result = environment . getUserScriptCurrentWorkingDirectory ( gitDirectory )
820+
821+ assertEquals ( result , gitDirectory )
822+ } )
823+
824+ Deno . test ( "getUserScriptCurrentWorkingDirectory - should return git directory when input is empty string" , ( ) => {
825+ Deno . env . set ( "INPUT_CURRENT_WORKING_DIRECTORY" , "" )
826+ const gitDirectory = Deno . cwd ( )
827+
828+ const result = environment . getUserScriptCurrentWorkingDirectory ( gitDirectory )
829+
830+ assertEquals ( result , gitDirectory )
831+ } )
832+
833+ Deno . test ( "getUserScriptCurrentWorkingDirectory - should return git directory when input is whitespace only" , ( ) => {
834+ Deno . env . set ( "INPUT_CURRENT_WORKING_DIRECTORY" , " " )
835+ const gitDirectory = Deno . cwd ( )
836+
837+ const result = environment . getUserScriptCurrentWorkingDirectory ( gitDirectory )
838+
839+ assertEquals ( result , gitDirectory )
840+ } )
841+
842+ Deno . test ( "getUserScriptCurrentWorkingDirectory - should throw error when absolute path is provided" , async ( ) => {
843+ const tempDir = await Deno . makeTempDir ( { prefix : "decaf-cwd-test-" } )
844+
845+ try {
846+ Deno . env . set ( "INPUT_CURRENT_WORKING_DIRECTORY" , tempDir )
847+ const gitDirectory = Deno . cwd ( )
848+
849+ let errorThrown = false
850+ let errorMessage = ""
851+ try {
852+ environment . getUserScriptCurrentWorkingDirectory ( gitDirectory )
853+ } catch ( error ) {
854+ errorThrown = true
855+ errorMessage = ( error as Error ) . message
856+ }
857+
858+ assertEquals ( errorThrown , true )
859+ assertEquals ( errorMessage . includes ( "must be a relative path" ) , true )
860+ assertEquals ( errorMessage . includes ( "not an absolute path" ) , true )
861+ } finally {
862+ await Deno . remove ( tempDir )
863+ Deno . env . delete ( "INPUT_CURRENT_WORKING_DIRECTORY" )
864+ }
865+ } )
866+
867+ Deno . test ( "getUserScriptCurrentWorkingDirectory - should resolve relative path when valid relative directory is provided" , async ( ) => {
868+ const tempDir = await Deno . makeTempDir ( { prefix : "decaf-cwd-test-" } )
869+
870+ try {
871+ // Create a subdirectory
872+ const subDir = `${ tempDir } /subdir`
873+ await Deno . mkdir ( subDir )
874+
875+ // Use tempDir as the git directory
876+ const gitDirectory = tempDir
877+
878+ Deno . env . set ( "INPUT_CURRENT_WORKING_DIRECTORY" , "subdir" )
879+
880+ const result = environment . getUserScriptCurrentWorkingDirectory ( gitDirectory )
881+
882+ // Should resolve to absolute path (with symlinks resolved)
883+ assertEquals ( result , Deno . realPathSync ( subDir ) )
884+ } finally {
885+ await Deno . remove ( tempDir , { recursive : true } )
886+ Deno . env . delete ( "INPUT_CURRENT_WORKING_DIRECTORY" )
887+ }
888+ } )
889+
890+ Deno . test ( "getUserScriptCurrentWorkingDirectory - should resolve relative path with .. navigation" , async ( ) => {
891+ const tempDir = await Deno . makeTempDir ( { prefix : "decaf-cwd-test-" } )
892+
893+ try {
894+ // Create nested directories: tempDir/a/b
895+ const dirA = `${ tempDir } /a`
896+ const dirB = `${ dirA } /b`
897+ await Deno . mkdir ( dirA )
898+ await Deno . mkdir ( dirB )
899+
900+ // Use dirB as the git directory
901+ const gitDirectory = dirB
902+
903+ // Set relative path that goes up one level
904+ Deno . env . set ( "INPUT_CURRENT_WORKING_DIRECTORY" , ".." )
905+
906+ const result = environment . getUserScriptCurrentWorkingDirectory ( gitDirectory )
907+
908+ // Should resolve to dirA (with symlinks resolved)
909+ assertEquals ( result , Deno . realPathSync ( dirA ) )
910+ } finally {
911+ await Deno . remove ( tempDir , { recursive : true } )
912+ Deno . env . delete ( "INPUT_CURRENT_WORKING_DIRECTORY" )
913+ }
914+ } )
915+
916+ Deno . test ( "getUserScriptCurrentWorkingDirectory - should throw error when directory does not exist" , ( ) => {
917+ const nonExistentDir = "this-directory-does-not-exist-" + Date . now ( )
918+ const gitDirectory = Deno . cwd ( )
919+
920+ Deno . env . set ( "INPUT_CURRENT_WORKING_DIRECTORY" , nonExistentDir )
921+
922+ let errorThrown = false
923+ let errorMessage = ""
924+ try {
925+ environment . getUserScriptCurrentWorkingDirectory ( gitDirectory )
926+ } catch ( error ) {
927+ errorThrown = true
928+ errorMessage = ( error as Error ) . message
929+ }
930+
931+ assertEquals ( errorThrown , true )
932+ assertEquals ( errorMessage . includes ( "does not exist" ) , true )
933+ assertEquals ( errorMessage . includes ( nonExistentDir ) , true )
934+
935+ Deno . env . delete ( "INPUT_CURRENT_WORKING_DIRECTORY" )
936+ } )
937+
938+ Deno . test ( "getUserScriptCurrentWorkingDirectory - should throw error when path is a file not a directory" , async ( ) => {
939+ const tempDir = await Deno . makeTempDir ( { prefix : "decaf-cwd-test-" } )
940+ const tempFile = `${ tempDir } /test-file.txt`
941+ await Deno . writeTextFile ( tempFile , "test content" )
942+
943+ try {
944+ // Use relative path to the file
945+ Deno . env . set ( "INPUT_CURRENT_WORKING_DIRECTORY" , "test-file.txt" )
946+ const gitDirectory = tempDir
947+
948+ let errorThrown = false
949+ let errorMessage = ""
950+ try {
951+ environment . getUserScriptCurrentWorkingDirectory ( gitDirectory )
952+ } catch ( error ) {
953+ errorThrown = true
954+ errorMessage = ( error as Error ) . message
955+ }
956+
957+ assertEquals ( errorThrown , true )
958+ assertEquals ( errorMessage . includes ( "is not a directory" ) , true )
959+ assertEquals ( errorMessage . includes ( "test-file.txt" ) , true )
960+ } finally {
961+ await Deno . remove ( tempDir , { recursive : true } )
962+ Deno . env . delete ( "INPUT_CURRENT_WORKING_DIRECTORY" )
963+ }
964+ } )
965+
966+ Deno . test ( "getUserScriptCurrentWorkingDirectory - should handle path with trailing slash" , async ( ) => {
967+ const tempDir = await Deno . makeTempDir ( { prefix : "decaf-cwd-test-" } )
968+ const subDir = `${ tempDir } /subdir`
969+ await Deno . mkdir ( subDir )
970+
971+ try {
972+ // Use relative path with trailing slash
973+ Deno . env . set ( "INPUT_CURRENT_WORKING_DIRECTORY" , "subdir/" )
974+ const gitDirectory = tempDir
975+
976+ const result = environment . getUserScriptCurrentWorkingDirectory ( gitDirectory )
977+
978+ // Should still work and return the directory (normalized and with symlinks resolved)
979+ assertEquals ( result , Deno . realPathSync ( subDir ) )
980+ } finally {
981+ await Deno . remove ( tempDir , { recursive : true } )
982+ Deno . env . delete ( "INPUT_CURRENT_WORKING_DIRECTORY" )
983+ }
984+ } )
985+
986+ Deno . test ( "getUserScriptCurrentWorkingDirectory - should handle current directory specified as ." , async ( ) => {
987+ const gitDirectory = Deno . cwd ( )
988+
989+ Deno . env . set ( "INPUT_CURRENT_WORKING_DIRECTORY" , "." )
990+
991+ const result = environment . getUserScriptCurrentWorkingDirectory ( gitDirectory )
992+
993+ assertEquals ( result , gitDirectory )
994+
995+ Deno . env . delete ( "INPUT_CURRENT_WORKING_DIRECTORY" )
996+ } )
997+
998+ Deno . test ( "getUserScriptCurrentWorkingDirectory - should trim whitespace from input" , async ( ) => {
999+ const tempDir = await Deno . makeTempDir ( { prefix : "decaf-cwd-test-" } )
1000+ const subDir = `${ tempDir } /subdir`
1001+ await Deno . mkdir ( subDir )
1002+
1003+ try {
1004+ // Use relative path with whitespace
1005+ Deno . env . set ( "INPUT_CURRENT_WORKING_DIRECTORY" , ` subdir ` )
1006+ const gitDirectory = tempDir
1007+
1008+ const result = environment . getUserScriptCurrentWorkingDirectory ( gitDirectory )
1009+
1010+ // Should trim whitespace and resolve symlinks
1011+ assertEquals ( result , Deno . realPathSync ( subDir ) )
1012+ } finally {
1013+ await Deno . remove ( tempDir , { recursive : true } )
1014+ Deno . env . delete ( "INPUT_CURRENT_WORKING_DIRECTORY" )
1015+ }
1016+ } )
0 commit comments