public inbox for ~johnnyrichard/olang-devel@lists.sr.ht
 help / color / mirror / code / Atom feed
From: Carlos Maniero <carlos@maniero.me>
To: ~johnnyrichard/olang-devel@lists.sr.ht
Cc: Carlos Maniero <carlos@maniero.me>
Subject: [PATCH olang 2/2] tests: add integration test setup
Date: Thu, 15 Feb 2024 13:21:46 -0300	[thread overview]
Message-ID: <20240215162146.847336-3-carlos@maniero.me> (raw)
In-Reply-To: <20240215162146.847336-1-carlos@maniero.me>

This is a basic setup for integration tests which includes:

- a *cli_runner* helper file to provide an interface to interact with
  the CLI.
- a simple test that validates if the compiler is returning zero status
  code when a file is provided.

At this point the compiler still doing nothing making all this bootstrap
just a fancy way to check if the compiler was compiled properly.

Signed-off-by: Carlos Maniero <carlos@maniero.me>
---
 .build.yml                     |  6 ++-
 Makefile                       | 16 ++++++++
 tests/integration/Makefile     | 27 +++++++++++++
 tests/integration/cli_runner.c | 71 ++++++++++++++++++++++++++++++++++
 tests/integration/cli_runner.h | 27 +++++++++++++
 tests/integration/cli_test.c   | 39 +++++++++++++++++++
 6 files changed, 185 insertions(+), 1 deletion(-)
 create mode 100644 tests/integration/Makefile
 create mode 100644 tests/integration/cli_runner.c
 create mode 100644 tests/integration/cli_runner.h
 create mode 100644 tests/integration/cli_test.c

diff --git a/.build.yml b/.build.yml
index 3aebfcf..7ed89eb 100644
--- a/.build.yml
+++ b/.build.yml
@@ -8,7 +8,11 @@ sources:
 tasks:
   - lint: |
       cd olang
-      make linter
+      make linter-all
   - build: |
       cd olang
       make
+  - integration-test: |
+      cd olang
+      make integration-test
+
diff --git a/Makefile b/Makefile
index 2a23b59..fa3df6c 100644
--- a/Makefile
+++ b/Makefile
@@ -20,9 +20,25 @@ $(BUILD_DIR):
 linter: $(SRCS) $(HEADERS)
 	clang-format --dry-run --Werror $?
 
+.PHONY: linter-all
+linter-all: $(SRCS) $(HEADERS)
+	make linter
+	make -C tests/integration/ linter
+
+
 .PHONY: linter-fix
 linter-fix: $(SRCS) $(HEADERS)
 	clang-format -i $?
 
+.PHONY: linter-fix-all
+linter-fix-all: $(SRCS) $(HEADERS)
+	make linter-fix
+	make -C tests/integration/ linter-fix
+
+.PHONY: integration-test
+integration-test:
+	make
+	make -C tests/integration/
+
 $(BUILD_DIR)/%.o: $(SRC_DIR)/%.c
 	$(CC) $(CFLAGS) -c $< -o $@
diff --git a/tests/integration/Makefile b/tests/integration/Makefile
new file mode 100644
index 0000000..a42f787
--- /dev/null
+++ b/tests/integration/Makefile
@@ -0,0 +1,27 @@
+SRCS        := $(wildcard *_test.c)
+TO_LINT     := $(filter-out munit.c munit.h,$(wildcard *.c *.h))
+OBJS        := $(patsubst %_test.c, %_test.o, $(SRCS))
+CFLAGS      := -I../../src
+TESTS       := $(patsubst %_test.c, %_test, $(SRCS))
+EXEC_TESTS  := $(patsubst %_test, ./%_test, $(TESTS))
+
+.PHONY: all
+all: munit.o cli_runner.o $(TESTS)
+	@for file in $(EXEC_TESTS); do \
+                ./"$$file"; \
+        done
+
+.PHONY: clean
+clean:
+	$(RM) *.o *_test
+
+.PHONY: linter
+linter: $(TO_LINT)
+	clang-format --dry-run --Werror $?
+
+.PHONY: linter-fix
+linter-fix: $(TO_LINT)
+	clang-format -i $?
+
+cli_test: munit.o cli_runner.o cli_test.o
+	$(CC) $? $(CFLAGS) -o $@
diff --git a/tests/integration/cli_runner.c b/tests/integration/cli_runner.c
new file mode 100644
index 0000000..cd7ba22
--- /dev/null
+++ b/tests/integration/cli_runner.c
@@ -0,0 +1,71 @@
+/*
+ * 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 <assert.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#define OLANG_COMPILER_PATH "../../0c"
+
+int _compiler_exists_already_checked = 0;
+
+void
+_assert_compiler_exists()
+{
+    {
+        if (_compiler_exists_already_checked == 1) {
+            return;
+        }
+
+        _compiler_exists_already_checked = 1;
+    }
+
+    FILE *file;
+    if ((file = fopen(OLANG_COMPILER_PATH, "r"))) {
+        fclose(file);
+        return;
+    }
+
+    assert(false && "Compiler not found. Build the compiler before executing tests.");
+}
+
+void
+_create_tmp_file_name(char *file_name)
+{
+    sprintf(file_name, "%s/olang_programXXXXXX", P_tmpdir);
+    int fd = mkstemp(file_name);
+    assert(fd != -1 && "Could not create a tmp file");
+    close(fd);
+}
+
+cli_result_t
+cli_runner_compile_file(char *src)
+{
+    _assert_compiler_exists();
+
+    cli_result_t result;
+    _create_tmp_file_name(result.binary_loc);
+
+    char command[1024];
+    sprintf(command, "%s -o %s %s", OLANG_COMPILER_PATH, result.binary_loc, src);
+
+    result.exit_code = system(command);
+    return result;
+}
diff --git a/tests/integration/cli_runner.h b/tests/integration/cli_runner.h
new file mode 100644
index 0000000..0df7f2d
--- /dev/null
+++ b/tests/integration/cli_runner.h
@@ -0,0 +1,27 @@
+/*
+ * 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
+typedef struct cli_result_t
+{
+    int exit_code;
+    char binary_loc[255];
+} cli_result_t;
+
+cli_result_t
+cli_runner_compile_file(char *src);
+#endif
diff --git a/tests/integration/cli_test.c b/tests/integration/cli_test.c
new file mode 100644
index 0000000..c7a9557
--- /dev/null
+++ b/tests/integration/cli_test.c
@@ -0,0 +1,39 @@
+/*
+ * 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"
+
+static MunitResult
+test_cli_hello_file(const MunitParameter params[], void *user_data_or_fixture)
+{
+    cli_result_t compilation_result = cli_runner_compile_file("../../examples/hello.olang");
+    munit_assert_int(compilation_result.exit_code, ==, 0);
+    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 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;
+}
-- 
2.34.1


  parent reply	other threads:[~2024-02-15 16:26 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-02-15 16:21 [PATCH olang 0/2] Add integration tests Carlos Maniero
2024-02-15 16:21 ` [PATCH olang 1/2] tests: add munit testing framework file Carlos Maniero
2024-02-15 16:21 ` Carlos Maniero [this message]
2024-02-15 16:27   ` [olang/patches/.build.yml] build success builds.sr.ht
2024-02-15 22:21   ` [PATCH olang 2/2] tests: add integration test setup Johnny Richard
2024-02-15 22:07     ` Carlos Maniero
2024-02-16  2:27     ` Carlos Maniero
2024-02-16  8:17       ` 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=20240215162146.847336-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