diff --git a/distribution/std-lib/Standard/src/Base/System/Process.enso b/distribution/std-lib/Standard/src/Base/System/Process.enso index 799239408fd6..1b4aa1669163 100644 --- a/distribution/std-lib/Standard/src/Base/System/Process.enso +++ b/distribution/std-lib/Standard/src/Base/System/Process.enso @@ -3,45 +3,55 @@ import Standard.Base.System.Process.Exit_Code from Standard.Base.Data.Vector import Vector from Builtins import Array, System, True, False -## The builder object that is used to create operating system processes. -type Process_Builder command arguments stdin +## UNSTABLE -## The result of the process invocation. -type Process_Result exit_code stdout stderr + The builder object that is used to create operating system processes. +type Builder + type Builder command arguments=[] stdin="" -## Call a system executable by name. + ## UNSTABLE - > Example - Call the `nproc` command: - Process.execute "nproc" - The result is printed to stdout: - 8 -Process.execute : String -> Exit_Code -Process.execute command = - result = System.create_process command (arguments = Array.empty) (input = "") (redirectIn = True) (redirectOut = True) (redirectErr = True) - Exit_Code.from_number result.exit_code + Sets the arguments that should be passed to the created process. + set_arguments : Vector.Vector Text -> Builder + set_arguments arguments = Builder this.command arguments this.stdin + + ## UNSTABLE + + Sets the text that will be used to feed standard input to the created + process. + set_stdin : Text -> Builder + set_stdin stdin = Builder this.command this.arguments stdin + + ## UNSTABLE + + Create a process using a builder returning the result of execution. -## Call a command with a list of arguments. + > Example + Create a script redirecting the input to stdout: + builder = Process.builder "bash" ["-c", "read line; echo -n $line"] "test" + builder.create + The result is: + Process_Result Exit_Success "test" "" + create : Result + create = + result = System.create_process this.command this.arguments.to_array this.stdin redirect_in=False redirect_out=False redirect_err=False + Result (Exit_Code.from_number result.exit_code) result.stdout result.stderr + +## UNSTABLE + + The result of the process invocation. +type Result exit_code stdout stderr + +## UNSTABLE + + Call a command with a list of arguments. > Example Call the `echo` command with arguments - Process.run_command "echo" ["-n", "Hello!"] + Process.run "echo" ["-n", "Hello!"] The result is printed to stdout: Hello! -Process.run_command : String -> Vector.Vector -> Exit_Code -Process.run_command command arguments = - result = System.create_process command arguments.to_array (input = "") (redirectIn = True) (redirectOut = True) (redirectErr = True) +run : Text -> Vector.Vector Text -> Exit_Code +run command arguments=[] = + result = System.create_process command arguments.to_array input="" redirect_in=True redirect_out=True redirect_err=True Exit_Code.from_number result.exit_code - -## Create a process using a builder returning the result of execution. - - > Example - Create a script redirecting the input to stdout: - builder = Process_Builder "bash" ["-c", "read line; echo -n $line"] "test" - Process.create_process builder - The result is: - Process_Result Exit_Success "test" "" -Process.create : Process_Builder -> Process_Result -Process.create b = - result = System.create_process b.command b.arguments.to_array b.stdin (redirectIn = False) (redirectOut = False) (redirectErr = False) - Process_Result (Exit_Code.from_number result.exit_code) result.stdout result.stderr diff --git a/engine/runtime/src/main/java/org/enso/interpreter/node/expression/builtin/system/CreateProcessNode.java b/engine/runtime/src/main/java/org/enso/interpreter/node/expression/builtin/system/CreateProcessNode.java index 9e6a47c5f20d..d2d9d2fe08aa 100644 --- a/engine/runtime/src/main/java/org/enso/interpreter/node/expression/builtin/system/CreateProcessNode.java +++ b/engine/runtime/src/main/java/org/enso/interpreter/node/expression/builtin/system/CreateProcessNode.java @@ -32,9 +32,9 @@ abstract Object execute( Object command, Array arguments, Object input, - boolean redirectIn, - boolean redirectOut, - boolean redirectErr); + boolean redirect_in, + boolean redirect_out, + boolean redirect_err); @Specialization @CompilerDirectives.TruffleBoundary diff --git a/test/Tests/src/System/Process_Spec.enso b/test/Tests/src/System/Process_Spec.enso index e3b44c882031..0514c08f120b 100644 --- a/test/Tests/src/System/Process_Spec.enso +++ b/test/Tests/src/System/Process_Spec.enso @@ -10,39 +10,35 @@ spec = Test.group "Process" <| Test.specify "should call simple command" <| result = case Platform.os of Platform.Windows -> - Process.run_command "PowerShell" ["-Command", "exit 0"] + Process.run "PowerShell" ["-Command", "exit 0"] _ -> - Process.run_command "bash" ["-c", "exit 0"] - case result of - Exit_Success -> Test.Success - Exit_Failure code -> Test.fail ("Process failed with code " + code) + Process.run "bash" ["-c", "exit 0"] + result.should_equal Exit_Success Test.specify "should return exit code" <| case Platform.os of Platform.Windows -> - case Process.run_command "PowerShell" ["-Command", "exit 42"] of - Exit_Success -> Test.fail "Process exist code mismatch" - Exit_Failure code -> code.should_equal 42 + r = Process.run "PowerShell" ["-Command", "exit 42"] + r.should_equal <| Exit_Failure 42 _ -> - case Process.run_command "bash" ["-c", "exit 42"] of - Exit_Success -> Test.fail "Process exit code mismatch." - Exit_Failure code -> code.should_equal 42 + r = Process.run "bash" ["-c", "exit 42"] + r.should_equal <| Exit_Failure 42 Test.specify "should return stdout" <| case Platform.os of Platform.Linux -> - builder = Process.Process_Builder "bash" ["-c", "echo -n Hello"] "" - result = Process.create builder + builder = Process.builder "bash" ["-c", "echo -n Hello"] + result = builder.create result.exit_code.to_number . should_equal 0 result.stdout . should_equal "Hello" result.stderr . should_equal "" Platform.MacOS -> - builder = Process.Process_Builder "bash" ["-c", "echo -n Hello"] "" - result = Process.create builder + builder = Process.builder "bash" ["-c", "echo -n Hello"] + result = builder.create result.exit_code.to_number . should_equal 0 result.stdout . should_equal "Hello" result.stderr . should_equal "" Platform.Windows -> - builder = Process.Process_Builder "PowerShell" ["-Command", "[System.Console]::Out.Write('Hello')"] "" - result = Process.create builder + builder = Process.builder "PowerShell" ["-Command", "[System.Console]::Out.Write('Hello')"] + result = builder.create result.exit_code.to_number . should_equal 0 result.stdout . should_equal "Hello" result.stderr . should_equal "" @@ -51,20 +47,20 @@ spec = Test.group "Process" <| Test.specify "should return stderr" <| case Platform.os of Platform.Linux -> - builder = Process.Process_Builder "bash" ["-c", "echo -n Error 1>&2"] "" - result = Process.create builder + builder = Process.builder "bash" ["-c", "echo -n Error 1>&2"] + result = builder.create result.exit_code.to_number . should_equal 0 result.stdout . should_equal "" result.stderr . should_equal "Error" Platform.MacOS -> - builder = Process.Process_Builder "bash" ["-c", "echo -n Error 1>&2"] "" - result = Process.create builder + builder = Process.builder "bash" ["-c", "echo -n Error 1>&2"] "" + result = builder.create result.exit_code.to_number . should_equal 0 result.stdout . should_equal "" result.stderr . should_equal "Error" Platform.Windows -> - builder = Process.Process_Builder "PowerShell" ["-Command", "[System.Console]::Error.Write('Error')"] "" - result = Process.create builder + builder = Process.builder "PowerShell" ["-Command", "[System.Console]::Error.Write('Error')"] "" + result = builder.create result.exit_code.to_number . should_equal 0 result.stdout . should_equal "" result.stderr . should_equal "Error" @@ -73,20 +69,20 @@ spec = Test.group "Process" <| Test.specify "should feed stdin" <| case Platform.os of Platform.Linux -> - builder = Process.Process_Builder "bash" ["-c", "read line; echo -n $line"] "sample" - result = Process.create builder + builder = Process.builder "bash" ["-c", "read line; echo -n $line"] . set_stdin "sample" + result = builder.create result.exit_code.to_number . should_equal 0 result.stdout . should_equal "sample" result.stderr . should_equal "" Platform.MacOS -> - builder = Process.Process_Builder "bash" ["-c", "read line; echo -n $line"] "sample" - result = Process.create builder + builder = Process.builder "bash" ["-c", "read line; echo -n $line"] . set_stdin "sample" + result = builder.create result.exit_code.to_number . should_equal 0 result.stdout . should_equal "sample" result.stderr . should_equal "" Platform.Windows -> - builder = Process.Process_Builder "PowerShell" ["-Command", "[System.Console]::ReadLine()"] "sample" - result = Process.create builder + builder = Process.builder "PowerShell" ["-Command", "[System.Console]::ReadLine()"] . set_stdin "sample" + result = builder.create result.exit_code.to_number . should_equal 0 result.stdout . should_equal 'sample\r\n' result.stderr . should_equal ""