* [PATCH olang v2 1/3] tests: fix: rename dump tokens test name
2024-03-08 22:39 [PATCH olang v2 0/3] test: cli: cover compilation pipeline Carlos Maniero
@ 2024-03-08 22:39 ` Carlos Maniero
2024-03-08 22:39 ` [PATCH olang v2 2/3] tests: decouple command execution from cli_runner Carlos Maniero
` (2 subsequent siblings)
3 siblings, 0 replies; 6+ messages in thread
From: Carlos Maniero @ 2024-03-08 22:39 UTC (permalink / raw)
To: ~johnnyrichard/olang-devel; +Cc: Carlos Maniero
The first integration test written was a placeholder to setup the
tooling.
The test content has changed when we introduced the dump tokens feature
but its name remains the same.
Signed-off-by: Carlos Maniero <carlos@maniero.me>
---
tests/integration/cli_test.c | 8 +++++---
1 file changed, 5 insertions(+), 3 deletions(-)
diff --git a/tests/integration/cli_test.c b/tests/integration/cli_test.c
index cb1a7df..f7c7417 100644
--- a/tests/integration/cli_test.c
+++ b/tests/integration/cli_test.c
@@ -19,7 +19,7 @@
#include "munit.h"
static MunitResult
-test_cli_hello_file(const MunitParameter params[], void *user_data_or_fixture)
+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);
@@ -41,8 +41,10 @@ test_cli_hello_file(const MunitParameter params[], void *user_data_or_fixture)
return MUNIT_OK;
}
-static MunitTest tests[] = { { "/test_cli_hello_file", test_cli_hello_file, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL },
- { NULL, NULL, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL } };
+static MunitTest tests[] = {
+ { "/test_cli_dump_tokens", test_cli_dump_tokens, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL },
+ { NULL, NULL, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL }
+};
static const MunitSuite suite = { "/cli_test", tests, NULL, 1, MUNIT_SUITE_OPTION_NONE };
--
2.34.1
^ permalink raw reply [flat|nested] 6+ messages in thread
* [PATCH olang v2 2/3] tests: decouple command execution from cli_runner
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
2024-03-08 22:39 ` [PATCH olang v2 3/3] tests: add tests for the minimal possible olang program Carlos Maniero
2024-03-08 23:59 ` [PATCH olang v2 0/3] test: cli: cover compilation pipeline Johnny Richard
3 siblings, 0 replies; 6+ messages in thread
From: Carlos Maniero @ 2024-03-08 22:39 UTC (permalink / raw)
To: ~johnnyrichard/olang-devel; +Cc: Carlos Maniero
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
^ permalink raw reply [flat|nested] 6+ messages in thread
* [PATCH olang v2 3/3] tests: add tests for the minimal possible olang program
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 ` [PATCH olang v2 2/3] tests: decouple command execution from cli_runner Carlos Maniero
@ 2024-03-08 22:39 ` 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
3 siblings, 1 reply; 6+ messages in thread
From: Carlos Maniero @ 2024-03-08 22:39 UTC (permalink / raw)
To: ~johnnyrichard/olang-devel; +Cc: Carlos Maniero
This test is just to ensure the cli is well set and it is compiling a
program given the appropriated flags.
Signed-off-by: Carlos Maniero <carlos@maniero.me>
---
tests/integration/cli_runner.c | 11 +++++++++++
tests/integration/cli_test.c | 21 +++++++++++++++++++++
2 files changed, 32 insertions(+)
diff --git a/tests/integration/cli_runner.c b/tests/integration/cli_runner.c
index 5a40e15..fed12ab 100644
--- a/tests/integration/cli_runner.c
+++ b/tests/integration/cli_runner.c
@@ -87,3 +87,14 @@ cli_runner_compiler_dump_tokens(char *src)
cli_runner_compiler(&result, program_args);
return result;
}
+
+cli_result_t
+cli_runner_compiler_compile(char *src)
+{
+ cli_result_t result = { 0 };
+ create_tmp_file_name(result.binary_path);
+
+ char *program_args[] = { "0c", src, "-o", result.binary_path, NULL };
+ cli_runner_compiler(&result, program_args);
+ return result;
+}
diff --git a/tests/integration/cli_test.c b/tests/integration/cli_test.c
index 126f612..c5896df 100644
--- a/tests/integration/cli_test.c
+++ b/tests/integration/cli_test.c
@@ -17,6 +17,7 @@
#define MUNIT_ENABLE_ASSERT_ALIASES
#include "cli_runner.h"
#include "munit.h"
+#include <stdio.h>
static MunitResult
test_cli_dump_tokens(const MunitParameter params[], void *user_data_or_fixture)
@@ -41,8 +42,28 @@ test_cli_dump_tokens(const MunitParameter params[], void *user_data_or_fixture)
return MUNIT_OK;
}
+static MunitResult
+test_cli_compile_minimal_program(const MunitParameter params[], void *user_data_or_fixture)
+{
+ cli_result_t compilation_result = cli_runner_compiler_compile("../../examples/main_exit.0");
+ munit_assert_int(compilation_result.exec.exit_code, ==, 0);
+
+ char *command_args[] = { compilation_result.binary_path, NULL };
+
+ proc_exec_command_t command = { .path = command_args[0], .args = command_args };
+
+ proc_exec(&command);
+
+ remove(command_args[0]);
+
+ munit_assert_int(command.result.exit_code, ==, 0);
+
+ return MUNIT_OK;
+}
+
static MunitTest tests[] = {
{ "/test_cli_dump_tokens", test_cli_dump_tokens, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL },
+ { "/test_cli_compile_minimal_program", test_cli_compile_minimal_program, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL },
{ NULL, NULL, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL }
};
--
2.34.1
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH olang v2 0/3] test: cli: cover compilation pipeline
2024-03-08 22:39 [PATCH olang v2 0/3] test: cli: cover compilation pipeline Carlos Maniero
` (2 preceding siblings ...)
2024-03-08 22:39 ` [PATCH olang v2 3/3] tests: add tests for the minimal possible olang program Carlos Maniero
@ 2024-03-08 23:59 ` Johnny Richard
3 siblings, 0 replies; 6+ messages in thread
From: Johnny Richard @ 2024-03-08 23:59 UTC (permalink / raw)
To: Carlos Maniero; +Cc: ~johnnyrichard/olang-devel
Great stuff! Applied.
To git.sr.ht:~johnnyrichard/olang
4684a78..f0909ec main -> main
^ permalink raw reply [flat|nested] 6+ messages in thread