Skip to content

Commit

Permalink
changed: switched back to execvp. using putenv now on Darwin/Linux
Browse files Browse the repository at this point in the history
  • Loading branch information
brendanfh committed Mar 3, 2024
1 parent 836fa07 commit 1b034ef
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 26 deletions.
12 changes: 8 additions & 4 deletions core/os/process.onyx
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,11 @@ ProcessResult :: enum {
InternalErr :: 0x03;
}

ProcessResultOutput :: struct {
result: ProcessResult;
output: str;
}


//
// Builder pattern for processes
Expand Down Expand Up @@ -164,14 +169,14 @@ command :: () -> &Command {
return cmd;
}

output :: (cmd: &Command) -> Result(str, ProcessResult) {
output :: (cmd: &Command) -> Result(str, ProcessResultOutput) {
if !cmd._process {
cmd._opts.capture_io = true;
cmd._opts.non_blocking_io = false;
cmd._opts.detach = false;

cmd->start();
if !cmd._process do return .{ Err = .Error };
if !cmd._process do return .{ Err = .{ .Error, "" } };
}

r := io.reader_make(cmd._process->unwrap_ptr());
Expand All @@ -180,8 +185,7 @@ command :: () -> &Command {

res := cmd->wait();
if res != .Success {
delete(&output);
return .{ Err = res };
return .{ Err = .{ res, output } };
}

return .{ Ok = output };
Expand Down
18 changes: 16 additions & 2 deletions runtime/src/ort_processes.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,23 @@ ONYX_DEF(__process_spawn, (WASM_I32, WASM_I32, WASM_I32, WASM_I32, WASM_I32, WAS
chdir(starting_dir); // Switch current working directory.
}

if (env) execve(process_path, process_args, env);
else execv(process_path, process_args);
if (env) {
char **env_walker = env;

while (*env_walker) {
// We have to duplicate the string here
// because there is disagreance on whether
// putenv copies its input or not. To be safe
// we copy it into the heap, which may be copied
// again by putenv :|
char *envvar = bh_strdup(bh_heap_allocator(), *env_walker);
putenv(envvar);

env_walker++;
}
}

execvp(process_path, process_args);
exit(-1);
break;

Expand Down
4 changes: 2 additions & 2 deletions scripts/onyx-pkg.onyx
Original file line number Diff line number Diff line change
Expand Up @@ -633,7 +633,7 @@ run_build_command :: (args: [] cstr) {
args << "-o";
args << bc.target;

p := os.process_spawn(os.path_join(os.env("ONYX_PATH") ?? "", "bin", "onyx"), args);
p := os.process_spawn("onyx", args);
r := io.reader_make(&p);
output := io.read_all(&r);
switch os.process_wait(&p) {
Expand Down Expand Up @@ -966,7 +966,7 @@ Package :: struct {
}

#if runtime.compiler_os == .Linux || runtime.compiler_os == .MacOS {
git_path :: "/usr/bin/git"
git_path :: "git"
}
#if runtime.compiler_os == .Windows {
git_path :: "git.exe"
Expand Down
39 changes: 21 additions & 18 deletions scripts/run_tests.onyx
Original file line number Diff line number Diff line change
Expand Up @@ -121,44 +121,47 @@ main :: (args) => {
use core {*}
print_color :: print_color;

args: [] str;
cmd := os.command()
->path(thread_data.onyx_cmd)
->env("ONYX_PATH", "./dist");

if thread_data.compile_only {
printf("[{}] Compiling test {}...\n", context.thread_id, it.source_file);
args = .["build", it.source_file];
cmd->args(.["build", it.source_file]);
} else {
printf("[{}] Running test {}...\n", context.thread_id, it.source_file);
args = .["run", it.source_file, "--generate-method-info"];
cmd->args(.["run", it.source_file, "--generate-method-info"]);
}

proc := os.process_spawn(thread_data.onyx_cmd, args);
defer os.process_destroy(&proc);

proc_reader := io.reader_make(&proc);
output := io.read_all(&proc_reader);
defer delete(&output);
output := cmd->output();
switch output {
case .Err as exit {
// Error running the test case
print_color(.Red, "[{}] Error '{}' in test case {}.\n{}", context.thread_id, exit.result, it.source_file, exit.output);
thread_data.at_least_one_test_failed = true;

if exit := os.process_wait(&proc); exit != .Success {
// Error running the test case
print_color(.Red, "[{}] Error '{}' in test case {}.\n{}", context.thread_id, exit, it.source_file, output);
thread_data.at_least_one_test_failed = true;
sync.critical_section(&thread_data.failed_tests_mutex) {
array.push(thread_data.failed_tests, it.source_file);
}

sync.critical_section(&thread_data.failed_tests_mutex) {
array.push(thread_data.failed_tests, it.source_file);
continue;
}

continue;
case .Ok ---
}

if thread_data.compile_only do continue;

program_output := output.Ok->unwrap();

for expected_file in os.with_file(it.expected_file) {
expected_reader := io.reader_make(expected_file);
expected_output := io.read_all(&expected_reader);

if output != expected_output {
if program_output != expected_output {
print_color(.Red, "[{}] Output did not match for {}.\n", context.thread_id, it.source_file);
printf("Expected:\n{}\n", expected_output);
printf("Got:\n{}\n", output);
printf("Got:\n{}\n", program_output);
thread_data.at_least_one_test_failed = true;

sync.critical_section(&thread_data.failed_tests_mutex) {
Expand Down

0 comments on commit 1b034ef

Please sign in to comment.