public inbox for ~johnnyrichard/olang-devel@lists.sr.ht
 help / color / mirror / code / Atom feed
* [PATCH olang 0/3] tests: cli: cover compilation pipeline
@ 2024-03-07 23:23 Carlos Maniero
  2024-03-07 23:23 ` [PATCH olang 1/3] tests: fix: rename dump tokens test name Carlos Maniero
                   ` (2 more replies)
  0 siblings, 3 replies; 7+ messages in thread
From: Carlos Maniero @ 2024-03-07 23:23 UTC (permalink / raw)
  To: ~johnnyrichard/olang-devel; +Cc: Carlos Maniero

This patch is focused in coverig the entire compilation pipeline.
Although as it is the second integration test of the olang codebase it
also has a few housekeeping in order to provide a smooth test
experience.

Carlos Maniero (3):
  tests: fix: rename dump tokens test name
  tests: decouple command execution from cli_runner
  tests: add tests for the minimal possible olang program

 tests/integration/Makefile     |  4 +--
 tests/integration/cli_runner.c | 60 +++++++++++++-------------------
 tests/integration/cli_runner.h | 10 ++++--
 tests/integration/cli_test.c   | 33 +++++++++++++++---
 tests/integration/proc_exec.c  | 62 ++++++++++++++++++++++++++++++++++
 tests/integration/proc_exec.h  | 36 ++++++++++++++++++++
 6 files changed, 158 insertions(+), 47 deletions(-)
 create mode 100644 tests/integration/proc_exec.c
 create mode 100644 tests/integration/proc_exec.h

-- 
2.34.1


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH olang 1/3] tests: fix: rename dump tokens test name
  2024-03-07 23:23 [PATCH olang 0/3] tests: cli: cover compilation pipeline Carlos Maniero
@ 2024-03-07 23:23 ` Carlos Maniero
  2024-03-07 23:23 ` [PATCH olang 2/3] tests: decouple command execution from cli_runner Carlos Maniero
  2024-03-07 23:23 ` [PATCH olang 3/3] tests: add tests for the minimal possible olang program Carlos Maniero
  2 siblings, 0 replies; 7+ messages in thread
From: Carlos Maniero @ 2024-03-07 23:23 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] 7+ messages in thread

* [PATCH olang 2/3] tests: decouple command execution from cli_runner
  2024-03-07 23:23 [PATCH olang 0/3] tests: cli: cover compilation pipeline Carlos Maniero
  2024-03-07 23:23 ` [PATCH olang 1/3] tests: fix: rename dump tokens test name Carlos Maniero
@ 2024-03-07 23:23 ` Carlos Maniero
  2024-03-08 22:24   ` Johnny Richard
  2024-03-07 23:23 ` [PATCH olang 3/3] tests: add tests for the minimal possible olang program Carlos Maniero
  2 siblings, 1 reply; 7+ messages in thread
From: Carlos Maniero @ 2024-03-07 23:23 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>
---
 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  | 62 ++++++++++++++++++++++++++++++++++
 tests/integration/proc_exec.h  | 36 ++++++++++++++++++++
 6 files changed, 122 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..2181ae0
--- /dev/null
+++ b/tests/integration/proc_exec.c
@@ -0,0 +1,62 @@
+/*
+ * 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_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]);
+
+        execv(command->path, command->args);
+        perror("execl error.");
+        exit(127);
+    } else {
+        close(fd_link[1]);
+        if (read(fd_link[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] 7+ messages in thread

* [PATCH olang 3/3] tests: add tests for the minimal possible olang program
  2024-03-07 23:23 [PATCH olang 0/3] tests: cli: cover compilation pipeline Carlos Maniero
  2024-03-07 23:23 ` [PATCH olang 1/3] tests: fix: rename dump tokens test name Carlos Maniero
  2024-03-07 23:23 ` [PATCH olang 2/3] tests: decouple command execution from cli_runner Carlos Maniero
@ 2024-03-07 23:23 ` Carlos Maniero
  2024-03-07 23:24   ` [olang/patches/.build.yml] build success builds.sr.ht
  2 siblings, 1 reply; 7+ messages in thread
From: Carlos Maniero @ 2024-03-07 23:23 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] 7+ messages in thread

* [olang/patches/.build.yml] build success
  2024-03-07 23:23 ` [PATCH olang 3/3] tests: add tests for the minimal possible olang program Carlos Maniero
@ 2024-03-07 23:24   ` builds.sr.ht
  0 siblings, 0 replies; 7+ messages in thread
From: builds.sr.ht @ 2024-03-07 23:24 UTC (permalink / raw)
  To: Carlos Maniero; +Cc: ~johnnyrichard/olang-devel

olang/patches/.build.yml: SUCCESS in 34s

[tests: cli: cover compilation pipeline][0] from [Carlos Maniero][1]

[0]: https://lists.sr.ht/~johnnyrichard/olang-devel/patches/50070
[1]: mailto:carlos@maniero.me

✓ #1164273 SUCCESS olang/patches/.build.yml https://builds.sr.ht/~johnnyrichard/job/1164273

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH olang 2/3] tests: decouple command execution from cli_runner
  2024-03-07 23:23 ` [PATCH olang 2/3] tests: decouple command execution from cli_runner Carlos Maniero
@ 2024-03-08 22:24   ` Johnny Richard
  2024-03-08 22:41     ` Carlos Maniero
  0 siblings, 1 reply; 7+ messages in thread
From: Johnny Richard @ 2024-03-08 22:24 UTC (permalink / raw)
  To: Carlos Maniero; +Cc: ~johnnyrichard/olang-devel

On Thu, Mar 07, 2024 at 08:23:21PM -0300, Carlos Maniero wrote:
> -        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) {

We are not reading the full ouptut here.  We can implement a logic to
read the whole string or leave a TODO comment here.

> 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];

Perhaps leave a TODO comment here about making it not sized? 
> +void
> +proc_exec(proc_exec_command_t* command)
> +{
> +    int fd_link[2];

nitpick: fd_link might miss leading to wrong understating.  What do you
think about changing it to fd_pipe to be clearer?

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH olang 2/3] tests: decouple command execution from cli_runner
  2024-03-08 22:24   ` Johnny Richard
@ 2024-03-08 22:41     ` Carlos Maniero
  0 siblings, 0 replies; 7+ messages in thread
From: Carlos Maniero @ 2024-03-08 22:41 UTC (permalink / raw)
  To: Johnny Richard; +Cc: ~johnnyrichard/olang-devel

I just sent a v2 with the fixes. Thanks.

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2024-03-08 22:41 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-03-07 23:23 [PATCH olang 0/3] tests: cli: cover compilation pipeline Carlos Maniero
2024-03-07 23:23 ` [PATCH olang 1/3] tests: fix: rename dump tokens test name Carlos Maniero
2024-03-07 23:23 ` [PATCH olang 2/3] tests: decouple command execution from cli_runner Carlos Maniero
2024-03-08 22:24   ` Johnny Richard
2024-03-08 22:41     ` Carlos Maniero
2024-03-07 23:23 ` [PATCH olang 3/3] tests: add tests for the minimal possible olang program Carlos Maniero
2024-03-07 23:24   ` [olang/patches/.build.yml] build success builds.sr.ht

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