From: Carlos Maniero <carlos@maniero.me>
To: ~johnnyrichard/olang-devel@lists.sr.ht
Cc: Carlos Maniero <carlos@maniero.me>
Subject: [PATCH olang 2/4] tests: remove previous integration tests structure
Date: Sun, 12 May 2024 11:30:31 -0300 [thread overview]
Message-ID: <20240512143033.229961-3-carlos@maniero.me> (raw)
In-Reply-To: <20240512143033.229961-1-carlos@maniero.me>
Signed-off-by: Carlos Maniero <carlos@maniero.me>
---
tests/integration/cli_runner.c | 100 -------------------------
tests/integration/cli_runner.h | 32 --------
tests/integration/cli_test.c | 132 ---------------------------------
tests/integration/proc_exec.c | 63 ----------------
tests/integration/proc_exec.h | 37 ---------
tests/integration/test.sh | 62 ++++++++--------
6 files changed, 33 insertions(+), 393 deletions(-)
delete mode 100644 tests/integration/cli_runner.c
delete mode 100644 tests/integration/cli_runner.h
delete mode 100644 tests/integration/cli_test.c
delete mode 100644 tests/integration/proc_exec.c
delete mode 100644 tests/integration/proc_exec.h
diff --git a/tests/integration/cli_runner.c b/tests/integration/cli_runner.c
deleted file mode 100644
index 636abfc..0000000
--- a/tests/integration/cli_runner.c
+++ /dev/null
@@ -1,100 +0,0 @@
-/*
- * 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 "cli_runner.h"
-#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>
-
-#define OLANG_COMPILER_PATH "../../olang"
-
-static int compiler_exists_already_checked = 0;
-
-static void
-assert_compiler_exists()
-{
- {
- if (compiler_exists_already_checked == 1) {
- return;
- }
-
- compiler_exists_already_checked = 1;
- }
-
- FILE *file = fopen(OLANG_COMPILER_PATH, "r");
-
- if (file != NULL) {
- fclose(file);
- return;
- }
-
- perror("Build the compiler before executing tests");
- exit(1);
-}
-
-void
-create_tmp_file_name(char *file_name)
-{
- sprintf(file_name, "%s/olang_programXXXXXX", P_tmpdir);
- int fd = mkstemp(file_name);
-
- if (fd == -1) {
- perror("Could not create a tmp file. Check your P_tmpdir permission.");
- exit(1);
- }
- close(fd);
-}
-
-void
-cli_runner_compiler(cli_result_t* result, char *args[])
-{
- assert_compiler_exists();
-
- proc_exec_command_t command = {
- .path = OLANG_COMPILER_PATH,
- .args = args
- };
-
- proc_exec(&command);
-
- result->exec = command.result;
-}
-
-cli_result_t
-cli_runner_compiler_dump_tokens(char *src)
-{
- cli_result_t result = { 0 };
-
- char *program_args[] = { "olang", "--dump-tokens", src, NULL };
- 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[] = { "olang", src, "-o", result.binary_path, NULL };
- cli_runner_compiler(&result, program_args);
- return result;
-}
diff --git a/tests/integration/cli_runner.h b/tests/integration/cli_runner.h
deleted file mode 100644
index 785cd34..0000000
--- a/tests/integration/cli_runner.h
+++ /dev/null
@@ -1,32 +0,0 @@
-/*
- * 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 CLI_RUNNER_H
-#define CLI_RUNNER_H
-#include "proc_exec.h"
-
-typedef struct cli_result_t
-{
- 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
deleted file mode 100644
index e7ae059..0000000
--- a/tests/integration/cli_test.c
+++ /dev/null
@@ -1,132 +0,0 @@
-/*
- * 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/>.
- */
-#define MUNIT_ENABLE_ASSERT_ALIASES
-#include "cli_runner.h"
-#include "munit.h"
-#include <stdio.h>
-
-static MunitResult
-test_cli_dump_tokens_example_main_exit(const MunitParameter params[], void *user_data_or_fixture)
-{
- cli_result_t compilation_result = cli_runner_compiler_dump_tokens("../../examples/main_exit.ol");
- munit_assert_int(compilation_result.exec.exit_code, ==, 0);
- munit_assert_string_equal(compilation_result.exec.stdout_buf,
- "../../examples/main_exit.ol:1:12: <line_feed>\n"
- "../../examples/main_exit.ol:2:16: <line_feed>\n"
- "../../examples/main_exit.ol:3:1: <line_feed>\n"
- "../../examples/main_exit.ol:4:1: <fn>\n"
- "../../examples/main_exit.ol:4:4: <identifier>\n"
- "../../examples/main_exit.ol:4:8: <(>\n"
- "../../examples/main_exit.ol:4:9: <)>\n"
- "../../examples/main_exit.ol:4:10: <:>\n"
- "../../examples/main_exit.ol:4:12: <identifier>\n"
- "../../examples/main_exit.ol:4:16: <{>\n"
- "../../examples/main_exit.ol:4:17: <line_feed>\n"
- "../../examples/main_exit.ol:5:3: <return>\n"
- "../../examples/main_exit.ol:5:10: <number>\n"
- "../../examples/main_exit.ol:5:11: <line_feed>\n"
- "../../examples/main_exit.ol:6:1: <}>\n"
- "../../examples/main_exit.ol:6:2: <line_feed>\n"
- "../../examples/main_exit.ol:7:1: <EOF>\n");
- return MUNIT_OK;
-}
-
-static MunitResult
-test_cli_dump_tokens_example_expression(const MunitParameter params[], void *user_data_or_fixture)
-{
- cli_result_t compilation_result = cli_runner_compiler_dump_tokens("../../examples/expression.ol");
- munit_assert_int(compilation_result.exec.exit_code, ==, 0);
- munit_assert_string_equal(compilation_result.exec.stdout_buf,
- "../../examples/expression.ol:1:1: <fn>\n"
- "../../examples/expression.ol:1:4: <identifier>\n"
- "../../examples/expression.ol:1:8: <(>\n"
- "../../examples/expression.ol:1:9: <)>\n"
- "../../examples/expression.ol:1:10: <:>\n"
- "../../examples/expression.ol:1:12: <identifier>\n"
- "../../examples/expression.ol:1:16: <{>\n"
- "../../examples/expression.ol:1:17: <line_feed>\n"
- "../../examples/expression.ol:2:3: <return>\n"
- "../../examples/expression.ol:2:10: <(>\n"
- "../../examples/expression.ol:2:11: <number>\n"
- "../../examples/expression.ol:2:14: <+>\n"
- "../../examples/expression.ol:2:16: <number>\n"
- "../../examples/expression.ol:2:18: <*>\n"
- "../../examples/expression.ol:2:20: <number>\n"
- "../../examples/expression.ol:2:21: <)>\n"
- "../../examples/expression.ol:2:23: <->\n"
- "../../examples/expression.ol:2:25: <(>\n"
- "../../examples/expression.ol:2:26: <number>\n"
- "../../examples/expression.ol:2:29: <->\n"
- "../../examples/expression.ol:2:31: <(>\n"
- "../../examples/expression.ol:2:32: <number>\n"
- "../../examples/expression.ol:2:34: <+>\n"
- "../../examples/expression.ol:2:36: <number>\n"
- "../../examples/expression.ol:2:37: <)>\n"
- "../../examples/expression.ol:2:39: </>\n"
- "../../examples/expression.ol:2:41: <number>\n"
- "../../examples/expression.ol:2:42: <)>\n"
- "../../examples/expression.ol:2:43: <line_feed>\n"
- "../../examples/expression.ol:3:1: <}>\n"
- "../../examples/expression.ol:3:2: <line_feed>\n"
- "../../examples/expression.ol:4:1: <EOF>\n");
- 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.ol");
- 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_example_main_exit",
- test_cli_dump_tokens_example_main_exit,
- NULL,
- NULL,
- MUNIT_TEST_OPTION_NONE,
- NULL },
- { "/test_cli_dump_tokens_example_expression",
- test_cli_dump_tokens_example_expression,
- 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 }
-};
-
-static const MunitSuite suite = { "/cli_test", tests, NULL, 1, MUNIT_SUITE_OPTION_NONE };
-
-int
-main(int argc, char *argv[])
-{
- return munit_suite_main(&suite, NULL, argc, argv);
- return EXIT_SUCCESS;
-}
diff --git a/tests/integration/proc_exec.c b/tests/integration/proc_exec.c
deleted file mode 100644
index c22dd1e..0000000
--- a/tests/integration/proc_exec.c
+++ /dev/null
@@ -1,63 +0,0 @@
-/*
- * 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
deleted file mode 100644
index 45c2977..0000000
--- a/tests/integration/proc_exec.h
+++ /dev/null
@@ -1,37 +0,0 @@
-/*
- * 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;
- // FIXME: output buffer shouldn't be fixed size
- char stdout_buf[2048];
-} 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
diff --git a/tests/integration/test.sh b/tests/integration/test.sh
index 6ab03de..9fdabef 100755
--- a/tests/integration/test.sh
+++ b/tests/integration/test.sh
@@ -1,3 +1,4 @@
+#!/bin/sh
COMPILER_PATH="../../olang"
TEST_FILE="$1"
@@ -15,57 +16,63 @@ TOKENS_EXPECT_OUTPUT_FILE="$TEST_FILE.test.expected_tokens_output"
TOKENS_OUTPUT_FILE="$TEST_FILE.test.tokens_output"
TOKENS_EXPECT_OUTPUT_FILE="$TEST_FILE.test.expected_tokens_output"
-PROGRAM_OUTPUT="$TEST_FILE.test.program_output"
-
+# Misc
extract_comment() {
tag="$1"
- comment="# $tag: "
- cat $TEST_FILE | grep "$comment" | sed -e "s/$comment//"
+ comment="# $tag:"
+ grep "$comment" "$TEST_FILE" | sed -e "s/$comment //"
+}
+
+cleanup() {
+ rm -f "$PROGRAM*"
}
# UI
COLOR_RED=1
COLOR_GREEN=2
COLOR_YELLOW=3
-COLOR_BLUE=4
COLOR_CYAN=6
COLOR_GRAY=7
colored() {
text="$1"
- color="$2"
+ color=$(tput setaf "$2")
+ reset=$(tput sgr0)
+
if [ -t 1 ]; then
- if tput setaf 1 &> /dev/null; then
- printf "$(tput setaf $color)$text$(tput sgr0)"
+ if tput setaf 1 > /dev/null 2>&1; then
+ printf "%s%s%s" "$color" "$text" "$reset"
return
fi
fi
- printf "$text"
+ printf "%s" "$text"
}
print_test_description() {
- colored "$TEST_FILE: \n" $COLOR_CYAN
- colored "$(extract_comment spec)\n\n" $COLOR_GRAY
+ colored "$TEST_FILE: " $COLOR_CYAN
+ printf "\n"
+ colored "$(extract_comment spec)" $COLOR_GRAY
+ printf "\n\n"
}
print_passed() {
context="$1"
- printf "$context: "
+ printf "%s: " "$context"
colored "passed" $COLOR_GREEN
echo
}
print_failed() {
context="$1"
- printf "$context: "
+ printf "%s: " "$context"
colored "failed" $COLOR_RED
echo
}
print_skiped() {
context="$1"
- printf "$context: "
+ printf "%s: " "$context"
colored "not set" $COLOR_GRAY
echo
}
@@ -98,7 +105,7 @@ expect_output() {
expected_file="$2"
actual_file="$3"
- if [ "$(cat $expected_file | wc -l)" = "0" ]; then
+ if [ "$(wc -l < "$expected_file")" = "0" ]; then
print_skiped "$context"
return
fi
@@ -107,7 +114,7 @@ expect_output() {
print_passed "$context"
else
if [ "$(cat "$expected_file")" = "%empty%" ]; then
- if [ "$(cat $actual_file | wc -c)" = "0" ]; then
+ if [ "$(wc -c < "$actual_file")" = "0" ]; then
print_passed "$context"
return
fi
@@ -119,19 +126,16 @@ expect_output() {
fi
}
-cleanup() {
- rm -f $PROGRAM*
-}
-
+# Tests
test_compiler() {
- $COMPILER_PATH $TEST_FILE -o $PROGRAM > "$COMPILER_OUTPUT_FILE" 2>&1
+ $COMPILER_PATH "$TEST_FILE" -o "$PROGRAM" > "$COMPILER_OUTPUT_FILE" 2>&1
EXIT=$?
EXPECTED_EXIT=$(extract_comment compiler_exit)
expect_exit_code compiler_exit "$EXPECTED_EXIT" "$EXIT"
- extract_comment compiler_output > $COMPILER_EXPECT_OUTPUT_FILE
+ extract_comment compiler_output > "$COMPILER_EXPECT_OUTPUT_FILE"
expect_output compiler_output "$COMPILER_EXPECT_OUTPUT_FILE" "$COMPILER_OUTPUT_FILE"
if [ "$EXIT" = "1" ]; then
@@ -142,25 +146,25 @@ test_compiler() {
}
test_ast() {
- $COMPILER_PATH $TEST_FILE --dump-ast > "$AST_OUTPUT_FILE" 2>&1
+ $COMPILER_PATH "$TEST_FILE" --dump-ast > "$AST_OUTPUT_FILE" 2>&1
- extract_comment ast > $AST_EXPECT_OUTPUT_FILE
+ extract_comment ast > "$AST_EXPECT_OUTPUT_FILE"
expect_output "ast" "$AST_EXPECT_OUTPUT_FILE" "$AST_OUTPUT_FILE"
}
test_tokens() {
- extract_comment tokens > $TOKENS_EXPECT_OUTPUT_FILE
- TOKEN_LINES=$(cat $TOKENS_EXPECT_OUTPUT_FILE | wc -l)
+ extract_comment tokens > "$TOKENS_EXPECT_OUTPUT_FILE"
+ TOKEN_LINES=$(wc -l < "$TOKENS_EXPECT_OUTPUT_FILE")
- $COMPILER_PATH $TEST_FILE --dump-tokens | head -n $TOKEN_LINES > "$TOKENS_OUTPUT_FILE" 2>&1
+ $COMPILER_PATH "$TEST_FILE" --dump-tokens | head -n "$TOKEN_LINES" > "$TOKENS_OUTPUT_FILE" 2>&1
- expect_output tokens $TOKENS_OUTPUT_FILE $TOKENS_EXPECT_OUTPUT_FILE
+ expect_output tokens "$TOKENS_OUTPUT_FILE" "$TOKENS_EXPECT_OUTPUT_FILE"
print_passed tokens
}
test_program() {
- ./$PROGRAM
+ "./$PROGRAM"
EXIT=$?
EXPECTED_EXIT=$(extract_comment program_exit)
--
2.34.1
next prev parent reply other threads:[~2024-05-12 14:30 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-05-12 14:30 [PATCH olang 0/4] comment based integration tests Carlos Maniero
2024-05-12 14:30 ` [PATCH olang 1/4] tests: add comment based integration tests mechanism Carlos Maniero
2024-05-12 14:30 ` Carlos Maniero [this message]
2024-05-12 14:30 ` [PATCH olang 3/4] tests: include integration tests for function parser errors Carlos Maniero
2024-05-12 14:30 ` [PATCH olang 4/4] tests: print integration tests TODOs Carlos Maniero
2024-05-12 14:31 ` [olang/patches/.build.yml] build success builds.sr.ht
2024-08-09 19:42 ` [PATCH olang 0/4] comment based integration tests Johnny Richard
2024-08-21 15:58 ` 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=20240512143033.229961-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