@@ -30,23 +30,57 @@ public void ShouldInitializeScriptFolder()
3030 }
3131
3232 [ Fact ]
33- public void ShouldRunCsxScriptDirectly ( )
33+ public void ShouldRegisterToRunCsxScriptDirectly ( )
3434 {
3535 using ( var scriptFolder = new DisposableFolder ( ) )
3636 {
3737 var ( output , exitCode ) = ScriptTestRunner . Default . Execute ( "init" , scriptFolder . Path ) ;
3838
3939 var scriptPath = Path . Combine ( scriptFolder . Path , "main.csx" ) ;
40+ if ( ScriptEnvironment . Default . IsWindows )
41+ {
42+ ( output , exitCode ) = ProcessHelper . RunAndCaptureOutput ( "reg.exe" , @"query HKCU\Software\Classes\.csx" ) ;
43+ Assert . Equal ( 0 , exitCode ) ;
44+ ( output , exitCode ) = ProcessHelper . RunAndCaptureOutput ( "reg.exe" , @"query HKCU\software\classes\dotnetscript" ) ;
45+ Assert . Equal ( 0 , exitCode ) ;
46+ }
47+ else
48+ {
49+ var text = File . ReadAllText ( scriptPath ) ;
50+ Assert . True ( text . StartsWith ( "#!/usr/bin/env dotnet-script" ) , "should have shebang" ) ;
51+ Assert . True ( text . IndexOf ( "\r \n " ) < 0 , "should have not have windows cr/lf" ) ;
52+ }
53+ }
54+ }
55+
56+ [ Fact ]
57+ public void ShouldRunCsxScriptDirectly ( )
58+ {
59+ using ( var scriptFolder = new DisposableFolder ( ) )
60+ {
61+ var scriptFolderPath = Path . Combine ( Path . GetTempPath ( ) , "xyz" ) ;
62+ Directory . CreateDirectory ( scriptFolderPath ) ;
63+ var ( output , exitCode ) = ScriptTestRunner . Default . Execute ( "init" , scriptFolderPath ) ;
64+
65+ var scriptPath = Path . Combine ( scriptFolderPath , "main.csx" ) ;
4066
4167 if ( ScriptEnvironment . Default . IsWindows )
4268 {
43- ( output , exitCode ) = ProcessHelper . RunAndCaptureOutput ( "cmd.exe" , $ "/c { scriptPath } ", scriptFolder . Path ) ;
69+ ( output , exitCode ) = ProcessHelper . RunAndCaptureOutput ( "cmd.exe" , $ "/c { scriptPath } ", scriptFolderPath ) ;
70+ Assert . Equal ( "Hello world!" , output . Trim ( ) ) ;
4471 }
4572 else
4673 {
47- ( output , exitCode ) = ProcessHelper . RunAndCaptureOutput ( scriptPath , string . Empty , scriptFolder . Path ) ;
74+ // this depends on dotnet-script being installed as a dotnet global tool because the shebang needs to
75+ // point to an executable in the environment. If you have dotnet-script installed as a global tool this
76+ // test will pass
77+ ( output , exitCode ) = ProcessHelper . RunAndCaptureOutput ( "dotnet-script" , $ "-h", scriptFolderPath ) ;
78+ if ( exitCode == 0 )
79+ {
80+ ( output , exitCode ) = ProcessHelper . RunAndCaptureOutput ( scriptPath , "" ) ;
81+ Assert . Equal ( "Hello world!" , output . Trim ( ) ) ;
82+ }
4883 }
49- Assert . Equal ( "Hello world!" , output . Trim ( ) ) ;
5084 }
5185 }
5286
0 commit comments