From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: mail-a.sr.ht; dkim=pass header.d=maniero.me header.i=@maniero.me Received: from dog.elm.relay.mailchannels.net (dog.elm.relay.mailchannels.net [23.83.212.48]) by mail-a.sr.ht (Postfix) with ESMTPS id D547520103 for <~johnnyrichard/olang-devel@lists.sr.ht>; Thu, 15 Feb 2024 16:26:50 +0000 (UTC) X-Sender-Id: hostingeremail|x-authuser|carlos@maniero.me Received: from relay.mailchannels.net (localhost [127.0.0.1]) by relay.mailchannels.net (Postfix) with ESMTP id 2B369825F7 for <~johnnyrichard/olang-devel@lists.sr.ht>; Thu, 15 Feb 2024 16:26:49 +0000 (UTC) Received: from nl-srv-smtpout4.hostinger.io (unknown [127.0.0.6]) (Authenticated sender: hostingeremail) by relay.mailchannels.net (Postfix) with ESMTPA id 7A379841A7 for <~johnnyrichard/olang-devel@lists.sr.ht>; Thu, 15 Feb 2024 16:26:48 +0000 (UTC) ARC-Seal: i=1; s=arc-2022; d=mailchannels.net; t=1708014408; a=rsa-sha256; cv=none; b=PFkNWKMQy560+0V4ewteP/90KbdgC+Miwpu4xNiSKCkvtCgrjVOPV0femoLFXRnAUDc2MG H2GAaTTnrdmsFYyZarKdjyYhENF5WmfH/oEp1tL1whWf49e681rui8T4HURzRmWLVoeFiI Ls7Q7+VyRqpaPFnOeHwzmwbyS/rBHlWPxr3XiQcabvF2JsrmYLxfFO1LjF3UcpkOv8OgkL y2kHwAuC/MK4Zsg/+H5pMaIQvs43hrzwmZAMrN/7dgR6nqb9PPpNTb2ceYubZD41TgjxIU w6CTWGqD4bhLal3n+PEItruQ9bvfBc1LLZ0Vr19fEHyUarWb4YpSbIHM7Iv/wQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=mailchannels.net; s=arc-2022; t=1708014408; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=oggY20jeBgZYOrKD7OMb/ZYZ3N2llpchc/14iRo8ppI=; b=dwjAttxih3kxyvthtpB064b9hVO84sASK0vZMIgnBpCglhXoWmzqfcvVXD2EQjM3zsZuPi oZMxdiBC4RWZ+AXS8l0IWYuhhUJ+hLfJcUEPtkfIuj7g7/WeO7Xpw0WIsvUJbQyZHB+lk2 FLMud/1n34qelEvKla+IkMpoxL5PnP4GkXkZUwZCtbC7f0Rbo72+33CfM5CwVhK16dWeWy spOSHCLhORHCiAIx94XQHPXc8XWXxlLoMtDiC/ZfArSzaqGNju8k9eXQxYVtD09rt/gvkd I/Ic6O+gvG64IuQ6+P6mHRGXM5T1WJdm0Qa7Tf+Jn5B/+/8qDk8PWna5NON1ZQ== ARC-Authentication-Results: i=1; rspamd-6bdc45795d-rmd4n; auth=pass smtp.auth=hostingeremail smtp.mailfrom=carlos@maniero.me X-Sender-Id: hostingeremail|x-authuser|carlos@maniero.me X-MC-Relay: Neutral X-MailChannels-SenderId: hostingeremail|x-authuser|carlos@maniero.me X-MailChannels-Auth-Id: hostingeremail X-Name-Obese: 416849993a1a7a2f_1708014409024_3347063101 X-MC-Loop-Signature: 1708014409023:3534994919 X-MC-Ingress-Time: 1708014409023 Received: from nl-srv-smtpout4.hostinger.io (nl-srv-smtpout4.hostinger.io [145.14.159.45]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384) by 100.119.153.75 (trex/6.9.2); Thu, 15 Feb 2024 16:26:49 +0000 From: Carlos Maniero DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=maniero.me; s=hostingermail1; t=1708014406; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=oggY20jeBgZYOrKD7OMb/ZYZ3N2llpchc/14iRo8ppI=; b=jyzMmRyUIeyIqRv7XaOJZtOVSuPHF62J9FaG2cWg++7+0PJkeFdZbUoO8hd1Vc9qbcN6rg Ta3bE0yGGTph8ALe8L9a2KaYpqgkvKO70fRNVdVNwT1lQn2Iu/LKYpE8aKSfK0bQi4IseS f3JN0E1fpLPdUSheVku1c/LE4EMoptc9h7m4aw1VbwiLhYiFbbfwJRZ/GY/0bc1dBSWEGX 3nM/DNyK+3HxgOkyjR/L3XQPohngzenyXEKvYG2bRNkq36s+uX60pCni1incUHfYrqv4Bp DvDA9hBnSPp5kB+bc/gFEHHswc3+WlWRRQWa/XaXmKzz0Y9RCpB+L3JlbciPRw== To: ~johnnyrichard/olang-devel@lists.sr.ht Cc: Carlos Maniero Subject: [PATCH olang 2/2] tests: add integration test setup Date: Thu, 15 Feb 2024 13:21:46 -0300 Message-Id: <20240215162146.847336-3-carlos@maniero.me> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240215162146.847336-1-carlos@maniero.me> References: <20240215162146.847336-1-carlos@maniero.me> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-CM-Analysis: v=2.4 cv=DIOJ4TNb c=1 sm=1 tr=0 ts=65ce3b46 a=5+VMC1FZ3J4mVPAKpPmAqg==:117 a=5+VMC1FZ3J4mVPAKpPmAqg==:17 a=MKtGQD3n3ToA:10 a=1oJP67jkp3AA:10 a=BXDaF_L80NYA:10 a=mDV3o1hIAAAA:8 a=Jula4zDGgnm7667QEx4A:9 a=_FVE-zBwftR9WsbkzFJk:22 X-CM-Envelope: MS4xfPy2Dm7hryaKGnKr1DXTfR/i46Mr9qeHeuwxh1yp5SFf7r1KWkbs77//7Qq5ddIySjwb8dxQDC2GkcqcGSGdE0a4PEKSBm7w2RKcQBphVeDeQgRNeP+J u2JI1WIF0vI3GGfi4bX8dXKq599FljLWacQ4DcBrldfB3N48DNnobhDlugJd/ZNM0nl4wrcBi0bVWEdgpOg+ZC21AF2UKbcxpPwCCYB3sBafNHTH8/pUHhsa OyAXMyYnZrbjt51e9fTYoQ== X-AuthUser: carlos@maniero.me X-TUID: tPhoETOfQV/l 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 --- .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 . + */ +#include "cli_runner.h" +#include +#include +#include +#include +#include +#include + +#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 . + */ +#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 . + */ +#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