From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp1.migadu.com ([2001:41d0:1008:1e59::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms5.migadu.com with LMTPS id EKKCG4BM6mVEQgAAbAwnHQ (envelope-from ) for ; Fri, 08 Mar 2024 00:23:44 +0100 Received: from aspmx1.migadu.com ([2001:41d0:303:e224::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp1.migadu.com with LMTPS id 4IuxFIBM6mU5SAAA62LTzQ (envelope-from ) for ; Fri, 08 Mar 2024 00:23:44 +0100 X-Envelope-To: patches@johnnyrichard.com Authentication-Results: aspmx1.migadu.com; dkim=pass header.d=lists.sr.ht header.s=20240113 header.b=R8KtEncM; dkim=pass header.d=maniero.me header.s=hostingermail1 header.b=J3HSy7uF; spf=pass (aspmx1.migadu.com: domain of lists@sr.ht designates 46.23.81.152 as permitted sender) smtp.mailfrom=lists@sr.ht; dmarc=none; arc=pass ("mailchannels.net:s=arc-2022:i=1") ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=johnnyrichard.com; s=key1; t=1709853824; h=from:from:sender:sender: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:list-id: list-unsubscribe:list-subscribe:list-post:dkim-signature; bh=KblTj9oHqu51GTwDP72aWzfQRCh7qhiVR5JtVwcFveY=; b=GJJ+vCnJ0qO9TmRWAU2VqYggJ7oIP//PHbm7PPySVRYNUvCF30HZwsp2X2MELqd4cPqH/R eS8Sbl3oRRIKtQrjQtjgnUiv+90iilltcqcvEXDTdHKC1RJIB0YtmJCjwvhTf1JgA4QxUD CGWaoTlQFKAng9Kw8XqP2oMx4N/I6C3wu6YYhda3ngEUA1j8W0mrKFSFuGA0zAKXQnPLrz sZ1bgfwwNja5grm4NOidlJV4PLs7zV0Kacnw6pOP6/laUP/1fkanGNSpn6C0pSNcekUm4N glbrF5K4wDrGlhmDiMpGafjbbMWs093XM1Zs1CXQHfLBCP9bq/ZBoeC+J72VYQ== ARC-Authentication-Results: i=2; aspmx1.migadu.com; dkim=pass header.d=lists.sr.ht header.s=20240113 header.b=R8KtEncM; dkim=pass header.d=maniero.me header.s=hostingermail1 header.b=J3HSy7uF; spf=pass (aspmx1.migadu.com: domain of lists@sr.ht designates 46.23.81.152 as permitted sender) smtp.mailfrom=lists@sr.ht; dmarc=none; arc=pass ("mailchannels.net:s=arc-2022:i=1") ARC-Seal: i=2; s=key1; d=johnnyrichard.com; t=1709853824; a=rsa-sha256; cv=pass; b=eZ7Kslk46tea7rObOSz+14A+6hd8MgP9FEdCrZf3W5DWFXjD0EG+ANwOia1+iOQemO2V1l EM/Q+SH18ori0EzsZBOvwWK50Tve1vDGBoFF8bVUYIIjVK3pg5StFLCSe9qWU6d/EDJYZY 8bZvm7kCFskuNCoG7hFoX/A5ACv7IVb5C5PJivFoHrD3kLrIjncJqYGh00Ah+oSVVAun02 q2GASrCf+6PWM5gqvLFxF8DWi0vawSWDHwLP83oKCZkiFYWk9STLqDLoBxuwr5rA+yTpEK f+XeSGNqrEMNiamSztOOZVhIYVc4I145AwXFjGKV53CK1DE5higC+8cmIGfH3Q== Received: from mail-a.sr.ht (mail-a.sr.ht [46.23.81.152]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by aspmx1.migadu.com (Postfix) with ESMTPS id 28F7E172CD for ; Fri, 8 Mar 2024 00:23:44 +0100 (CET) DKIM-Signature: a=rsa-sha256; bh=cdVtZLloHdRcrcWpa6pnRTpOCBbIIWiOhnBwb4zBkCc=; c=simple/simple; d=lists.sr.ht; h=From:To:Cc:Subject:Date:In-Reply-To:References:List-Unsubscribe:List-Subscribe:List-Archive:List-Post:List-ID; q=dns/txt; s=20240113; t=1709853824; v=1; b=R8KtEncMpVo3P1bnmWQbqu6H+rIzDgSVKynJ0Sgt70SBv81DOitlf5Sc6zKoD4UU29FkxGKF HUMuK5DZG5Onrgn0s5SBtgrhy4UYca1KVQyGiIjzbdwpWUcsumsavvMQSR0xP/3L1Vj7mu1A7xK h5nFv0QXXisHePbuzTIMOFVHAb7IMMvH99sZhBhm35Y2hjPTsbQaN/h47ihAGemgG4r2V0ntpP2 y6fPPp7VymtyycPd3HMXduQEJfQbKSDMdaaSMfwEopQeltf1/304c/mZkam5nLpHx/9lVxOxed2 urA0LEOr3U3ww/adrKG3PuHiDBz6VJQAbjQWZ0gjJ9f3g== Received: from lists.sr.ht (unknown [46.23.81.154]) by mail-a.sr.ht (Postfix) with ESMTPSA id 0D7A720162 for ; Thu, 7 Mar 2024 23:23:44 +0000 (UTC) Received: from seashell.cherry.relay.mailchannels.net (seashell.cherry.relay.mailchannels.net [23.83.223.162]) by mail-a.sr.ht (Postfix) with ESMTPS id 194A320115 for <~johnnyrichard/olang-devel@lists.sr.ht>; Thu, 7 Mar 2024 23:23:42 +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 80725361EEA for <~johnnyrichard/olang-devel@lists.sr.ht>; Thu, 7 Mar 2024 23:23:41 +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 A83243621CD for <~johnnyrichard/olang-devel@lists.sr.ht>; Thu, 7 Mar 2024 23:23:40 +0000 (UTC) ARC-Seal: i=1; s=arc-2022; d=mailchannels.net; t=1709853820; a=rsa-sha256; cv=none; b=U3vN91cnM32/g89FEKgqvP7XKb5kfikEW8uje7Yeh9WrZyrApno+aR3hEaSRTvw+sq/hnO iHmmvC58Vocl+0/NlIDFKajXBZqy2OsIdtluxFbIjWZEIzizFukRBKuEeKKVxcAowpWohm sfa3celCE3DjuXTcyLKh7zx1HTVUo4oBaqp7ETtP0n5q5YDZE35jRJi8bGgRFuxosElToJ XZ0HQIptwzIQfLV/SkOEmMYccO2Y01kyFK3TW5q82XmwTW3X+LJn+grmyyZ5kkqgEXNaVm tei4KuZWtBOGPUP7g9X2Z6C88KHs1V6YDIp4fmRFum+/4FH28hoRPqx4GNf5nA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=mailchannels.net; s=arc-2022; t=1709853820; 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=KblTj9oHqu51GTwDP72aWzfQRCh7qhiVR5JtVwcFveY=; b=sjT6uysHNWyBnGy56gjlt/9wM93VHXxo7YcDP3hrpWfwUtXRA3k5EX2eGTlVNQxH6zm4ma ZKICww+cClqvw2aGitFery8xGqyCLHHG9Ah2nudCQ0rnhl58hL6lVkuo5EdXI8lZhvItDs XupsLVyiFUSWj4I1zyoRU9pnVJKNj/2YBV/eVUVb+j7fT1sEwp3t8V4IhZPZCbyBhl2Dsx wg3B0eqiR81pRTteFVSwcYue9YECMaLF9w+/OY7Se6JnUHB2UeZqasmb0uZWksq1NHJRDA nj4ZRzq5NtH4+IPKUWDVCFGwUz7oOEzx6zZIzhYHl7d29hJqP/UKVbwqbEDE1Q== ARC-Authentication-Results: i=1; rspamd-5db57bc4b6-w5dwg; 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-Bitter-Spicy: 727a1a586c60d06d_1709853821371_277745359 X-MC-Loop-Signature: 1709853821371:2503733837 X-MC-Ingress-Time: 1709853821371 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.111.52.199 (trex/6.9.2); Thu, 07 Mar 2024 23:23:41 +0000 From: Carlos Maniero DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=maniero.me; s=hostingermail1; t=1709853819; 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=KblTj9oHqu51GTwDP72aWzfQRCh7qhiVR5JtVwcFveY=; b=J3HSy7uF2/Bobx4glaxGgb0L2Yf7TE+6b/W1PwUjow+lRFYraa09rFeAyYFW+aT5M8LgnP nufe/9EmuXZpJj6swClipsNS4H4ehKrpj9iil7FZwUIS6S22lOMBQyr1Wc1+V5MgEOALrx Oyfrzxrareg7wPaSJnPn7HlPkUgiNzj4wIDb8YgBr5sQFwp34uEoT6ckSz4zJeXNxyC9y9 6kObjx1++kEhjoQc05AyEEtCCF26Wp9c2V4W22JFugcad39LMGLzYZT1VpbJQbYvhRHZFs NNAfD7ozoAT3czUr5xuAZ4dOJqUhSxLAQIkQyD8zNVi+Olqwx6cS/Ai8J2SvBg== To: ~johnnyrichard/olang-devel@lists.sr.ht Cc: Carlos Maniero Subject: [PATCH olang 2/3] tests: decouple command execution from cli_runner Date: Thu, 7 Mar 2024 20:23:21 -0300 Message-Id: <20240307232322.2085290-3-carlos@maniero.me> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240307232322.2085290-1-carlos@maniero.me> References: <20240307232322.2085290-1-carlos@maniero.me> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-CM-Envelope: MS4xfNVjhYYIasOxDfU7lKVvgzCdbXDYbQHPxkcTXJCtfhEDFsc8wUJ5Q9LO3QaXoCV9VbN6BAeUkDTQyIWt3R6oEkmKstlu0++Oi6g5PyDFtwe7ReOxC+1J j0DFMVvgjXDF/PhQdQuybEUNtZbvTPi9tY/LUAjEdpf4QmYk88D979/18u7weMCLlu3ML1yX9ia1me4le2pB64X2kz3SkIFzfqM3EBRikAIlZxRmPGBY15th X-CM-Analysis: v=2.4 cv=SNJEVvvH c=1 sm=1 tr=0 ts=65ea4c7b a=5+VMC1FZ3J4mVPAKpPmAqg==:117 a=5+VMC1FZ3J4mVPAKpPmAqg==:17 a=MKtGQD3n3ToA:10 a=1oJP67jkp3AA:10 a=mDV3o1hIAAAA:8 a=jV5FX1hQg6-HhU48JFMA:9 a=_FVE-zBwftR9WsbkzFJk:22 a=BXDaF_L80NY05PYiAFlV:22 X-AuthUser: carlos@maniero.me X-Sourcehut-Patchset-Status: UNKNOWN List-Unsubscribe: List-Subscribe: List-Archive: Archived-At: List-Post: List-ID: ~johnnyrichard/olang-devel <~johnnyrichard/olang-devel.lists.sr.ht> Sender: ~johnnyrichard/olang-devel <~johnnyrichard/olang-devel@lists.sr.ht> X-Migadu-Country: NL X-Migadu-Flow: FLOW_IN X-Migadu-Scanner: mx12.migadu.com X-Migadu-Spam-Score: -9.54 X-Spam-Score: -9.54 X-Migadu-Queue-Id: 28F7E172CD X-TUID: 9goQbqvnkCtl 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 --- 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 . */ #include "cli_runner.h" +#include "proc_exec.h" #include #include #include @@ -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: \n" "../../examples/main_exit.0:1:4: \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 . + */ +#include "proc_exec.h" +#include +#include +#include +#include +#include +#include +#include + + +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 . + */ +#ifndef PROC_EXEC_H +#define PROC_EXEC_H +#include + +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