From: Carlos Maniero <carlos@maniero.me>
To: ~johnnyrichard/olang-devel@lists.sr.ht
Cc: Carlos Maniero <carlos@maniero.me>
Subject: [PATCH olang v2 2/3] tests: decouple command execution from cli_runner
Date: Fri, 8 Mar 2024 19:39:08 -0300 [thread overview]
Message-ID: <20240308223909.3184226-3-carlos@maniero.me> (raw)
In-Reply-To: <20240308223909.3184226-1-carlos@maniero.me>
This commit separates the logic for executing external programs from the
cli_runner. Previously, the abstraction for handling external programs
was tightly coupled with cli_runner.
However, during integration tests, there are numerous instances where we
need to run an external program. To address this, we introduce a new
function, proc_exec, which is dedicated to managing the execution of
external programs.
Signed-off-by: Carlos Maniero <carlos@maniero.me>
---
V2:
- Add a comment to stop truncating the stdout
- Rename fd_pipe variable
tests/integration/Makefile | 4 +--
tests/integration/cli_runner.c | 51 +++++++--------------------
tests/integration/cli_runner.h | 10 ++++--
tests/integration/cli_test.c | 4 +--
tests/integration/proc_exec.c | 63 ++++++++++++++++++++++++++++++++++
tests/integration/proc_exec.h | 36 +++++++++++++++++++
6 files changed, 123 insertions(+), 45 deletions(-)
create mode 100644 tests/integration/proc_exec.c
create mode 100644 tests/integration/proc_exec.h
diff --git a/tests/integration/Makefile b/tests/integration/Makefile
index eeebfdd..db2b7d9 100644
--- a/tests/integration/Makefile
+++ b/tests/integration/Makefile
@@ -7,7 +7,7 @@ MUNIT_SRC := ../shared/munit.c
MUNIT := ./munit.o
.PHONY: all
-all: $(MUNIT) cli_runner.o $(TESTS)
+all: $(MUNIT) proc_exec.o cli_runner.o $(TESTS)
@for file in $(EXEC_TESTS); do \
./"$$file"; \
done
@@ -24,7 +24,7 @@ linter: $(SRCS)
linter-fix: $(SRCS)
clang-format -i $?
-cli_test: $(MUNIT) cli_runner.o cli_test.o
+cli_test: $(MUNIT) proc_exec.o cli_runner.o cli_test.o
$(CC) $? $(CFLAGS) -o $@
$(MUNIT):
diff --git a/tests/integration/cli_runner.c b/tests/integration/cli_runner.c
index 7e4fe9a..5a40e15 100644
--- a/tests/integration/cli_runner.c
+++ b/tests/integration/cli_runner.c
@@ -15,6 +15,7 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "cli_runner.h"
+#include "proc_exec.h"
#include <assert.h>
#include <stdbool.h>
#include <stdio.h>
@@ -62,53 +63,27 @@ create_tmp_file_name(char *file_name)
close(fd);
}
-cli_result_t
-cli_runner_compiler(char *src, char *args[])
+void
+cli_runner_compiler(cli_result_t* result, char *args[])
{
assert_compiler_exists();
- cli_result_t result = { 0 };
- create_tmp_file_name(result.program_path);
-
- int fd_link[2];
-
- if (pipe(fd_link) == -1) {
- perror("pipe error.");
- exit(1);
- }
-
- pid_t pid = fork();
-
- if (pid == -1) {
- perror("fork error.");
- exit(1);
- }
-
- if (pid == 0) {
- dup2(fd_link[1], STDOUT_FILENO);
- close(fd_link[0]);
- close(fd_link[1]);
+ proc_exec_command_t command = {
+ .path = OLANG_COMPILER_PATH,
+ .args = args
+ };
- execv(OLANG_COMPILER_PATH, args);
- perror("execl error.");
- exit(127);
- } else {
- close(fd_link[1]);
- if (read(fd_link[0], result.compiler_output, sizeof(result.compiler_output)) == -1) {
- perror("read error.");
- exit(1);
- }
- int status;
- waitpid(pid, &status, 0);
- result.exit_code = WEXITSTATUS(status);
- }
+ proc_exec(&command);
- return result;
+ result->exec = command.result;
}
cli_result_t
cli_runner_compiler_dump_tokens(char *src)
{
+ cli_result_t result = { 0 };
+
char *program_args[] = { "0c", "--dump-tokens", src, NULL };
- return cli_runner_compiler(src, program_args);
+ cli_runner_compiler(&result, program_args);
+ return result;
}
diff --git a/tests/integration/cli_runner.h b/tests/integration/cli_runner.h
index 7ce4e7b..785cd34 100644
--- a/tests/integration/cli_runner.h
+++ b/tests/integration/cli_runner.h
@@ -16,13 +16,17 @@
*/
#ifndef CLI_RUNNER_H
#define CLI_RUNNER_H
+#include "proc_exec.h"
+
typedef struct cli_result_t
{
- int exit_code;
- char program_path[255];
- char compiler_output[1024];
+ char binary_path[255];
+ proc_exec_result_t exec;
} cli_result_t;
cli_result_t
cli_runner_compiler_dump_tokens(char *src);
+
+cli_result_t
+cli_runner_compiler_compile(char *src);
#endif
diff --git a/tests/integration/cli_test.c b/tests/integration/cli_test.c
index f7c7417..126f612 100644
--- a/tests/integration/cli_test.c
+++ b/tests/integration/cli_test.c
@@ -22,8 +22,8 @@ static MunitResult
test_cli_dump_tokens(const MunitParameter params[], void *user_data_or_fixture)
{
cli_result_t compilation_result = cli_runner_compiler_dump_tokens("../../examples/main_exit.0");
- munit_assert_int(compilation_result.exit_code, ==, 0);
- munit_assert_string_equal(compilation_result.compiler_output,
+ munit_assert_int(compilation_result.exec.exit_code, ==, 0);
+ munit_assert_string_equal(compilation_result.exec.stdout_buf,
"../../examples/main_exit.0:1:1: <fn>\n"
"../../examples/main_exit.0:1:4: <identifier>\n"
"../../examples/main_exit.0:1:8: <(>\n"
diff --git a/tests/integration/proc_exec.c b/tests/integration/proc_exec.c
new file mode 100644
index 0000000..c22dd1e
--- /dev/null
+++ b/tests/integration/proc_exec.c
@@ -0,0 +1,63 @@
+/*
+ * Copyright (C) 2024 olang maintainers
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ */
+#include "proc_exec.h"
+#include <assert.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/wait.h>
+#include <unistd.h>
+
+
+void
+proc_exec(proc_exec_command_t* command)
+{
+ int fd_pipe[2];
+
+ if (pipe(fd_pipe) == -1) {
+ perror("pipe error.");
+ exit(1);
+ }
+
+ pid_t pid = fork();
+
+ if (pid == -1) {
+ perror("fork error.");
+ exit(1);
+ }
+
+ if (pid == 0) {
+ dup2(fd_pipe[1], STDOUT_FILENO);
+ close(fd_pipe[0]);
+ close(fd_pipe[1]);
+
+ execv(command->path, command->args);
+ perror("execl error.");
+ exit(127);
+ } else {
+ close(fd_pipe[1]);
+ // TODO: stop truncating the output.
+ if (read(fd_pipe[0], command->result.stdout_buf, sizeof(command->result.stdout_buf)) == -1) {
+ perror("read error.");
+ exit(1);
+ }
+ int status;
+ waitpid(pid, &status, 0);
+ command->result.exit_code = WEXITSTATUS(status);
+ }
+}
diff --git a/tests/integration/proc_exec.h b/tests/integration/proc_exec.h
new file mode 100644
index 0000000..135aa6a
--- /dev/null
+++ b/tests/integration/proc_exec.h
@@ -0,0 +1,36 @@
+/*
+ * Copyright (C) 2024 olang maintainers
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ */
+#ifndef PROC_EXEC_H
+#define PROC_EXEC_H
+#include <stdlib.h>
+
+typedef struct proc_exec_result
+{
+ int exit_code;
+ char stdout_buf[1024];
+} proc_exec_result_t;
+
+typedef struct proc_exec_command
+{
+ char* path;
+ char** args;
+ proc_exec_result_t result;
+} proc_exec_command_t;
+
+void
+proc_exec(proc_exec_command_t* command);
+#endif
--
2.34.1
next prev parent reply other threads:[~2024-03-08 22:39 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-03-08 22:39 [PATCH olang v2 0/3] test: cli: cover compilation pipeline Carlos Maniero
2024-03-08 22:39 ` [PATCH olang v2 1/3] tests: fix: rename dump tokens test name Carlos Maniero
2024-03-08 22:39 ` Carlos Maniero [this message]
2024-03-08 22:39 ` [PATCH olang v2 3/3] tests: add tests for the minimal possible olang program Carlos Maniero
2024-03-08 22:40 ` [olang/patches/.build.yml] build success builds.sr.ht
2024-03-08 23:59 ` [PATCH olang v2 0/3] test: cli: cover compilation pipeline Johnny Richard
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20240308223909.3184226-3-carlos@maniero.me \
--to=carlos@maniero.me \
--cc=~johnnyrichard/olang-devel@lists.sr.ht \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://git.johnnyrichard.com/olang.git
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox