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 aMjiI6CT62UbTwAAbAwnHQ (envelope-from ) for ; Fri, 08 Mar 2024 23:39:28 +0100 Received: from aspmx1.migadu.com ([2001:41d0:403:4876::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp1.migadu.com with LMTPS id mMM+H6CT62XRgQEA62LTzQ (envelope-from ) for ; Fri, 08 Mar 2024 23:39:28 +0100 X-Envelope-To: patches@johnnyrichard.com Authentication-Results: aspmx1.migadu.com; dkim=pass header.d=lists.sr.ht header.s=20240113 header.b=ctsxy+dP; dkim=pass header.d=maniero.me header.s=hostingermail1 header.b=WUtu3upc; dmarc=none; arc=pass ("mailchannels.net:s=arc-2022:i=1"); spf=pass (aspmx1.migadu.com: domain of lists@sr.ht designates 46.23.81.152 as permitted sender) smtp.mailfrom=lists@sr.ht ARC-Seal: i=2; s=key1; d=johnnyrichard.com; t=1709937568; a=rsa-sha256; cv=pass; b=ROTrL5dzylDIch7j4T5kZCpUhwS603YTYtM8ouCYPREAxJC7LDxJtv5QOGaaOsReywbsIw uUZ9JQhZy1SotQkoeunTvdspJIUUj6tAWfnwqYapb7BJQrwmKjZMCAXFSqpsRDTAAggLzA 37M01xOUTf7/LUkoT3YgX/LGFH8Ko3ayaNXiDQ6ZZIHsuL5hDOkNCmK/6Qb50cdH6wIpBd J5sBAgKB7THoyYwm/HMeFlPg0Jpk6jrQkEerBzbjizZ+PDFBC+dUx6zJ7Fqrk2QFb7aoTG YmZ24TApRxpCyseBLOR0KYqp9vLyZdd9CmW85lDSyq2fqr5nUhuE09tvJ+7Ptw== ARC-Authentication-Results: i=2; aspmx1.migadu.com; dkim=pass header.d=lists.sr.ht header.s=20240113 header.b=ctsxy+dP; dkim=pass header.d=maniero.me header.s=hostingermail1 header.b=WUtu3upc; dmarc=none; arc=pass ("mailchannels.net:s=arc-2022:i=1"); spf=pass (aspmx1.migadu.com: domain of lists@sr.ht designates 46.23.81.152 as permitted sender) smtp.mailfrom=lists@sr.ht ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=johnnyrichard.com; s=key1; t=1709937568; 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=A8ZFrq1Rct97zut7NtvArxxNYnDDCnqP9JM2LNO/FNY=; b=RJqW/dSs1C0AmgPR5q07m84nbPRzlgbj+yNGRrjZyd1tZh2ElOkvv49ICpWqErgppZCI6U bXl17IBJEHALNqe+IO8t85Fm3YKy/230XwxyLznZOnVeOfBHZBHRhLRG7n+ZTAceHUvEEP NQj/BHLIqWuP4g+b55cqnVlv+2JEs3ncGZAwJtnWuIjrv4kWtPQdGVjr7bI4RFcgNSwNVJ Pb2chYlSc3zVmm6lJpgqoipvtiHOf2PSgT8tgoKcv6Hmz6IosWpSBOceLBt551peproSpp i4kRbxX1KkyAyi9bhZSMyFQ6ScJpJrgCjDMF6PP/ebyj4NRNO6d9JbSTUZXN0g== 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 126AF6A8C4 for ; Fri, 8 Mar 2024 23:39:28 +0100 (CET) DKIM-Signature: a=rsa-sha256; bh=XeYpwvYQK4I5IvHFzn3qXumsLwF3uWPqnoreher7liA=; 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=1709937567; v=1; b=ctsxy+dPpaxNJpVYCg95PsQ5lI01Dtr1AzCl1Lj/o/fadhnkVhkna32XP5TUn3jqh6Sai5cb OUVIi1wP1rKeAWlGt0Pmf1o6cnpPjib2duPsM2aZ/OKV1SfsPE4I/SYmIoLE5B+8uX/J9c9ZdG9 g11N8KA1wO4lQXFZSIcWIkQO6EaAyxC1VWZLAFUMIy0tztZb6MrzBfzZ90THjLUlUsn0+l9KBDM iP5sUTpYBQ2w5ByDZr3ZRfxNhWoQp90oIzRYXYF14CyCLKedSZCV/5vWT1KVQsH9aOiGhvDVmtK CZg/2tyOnqbVrcUPbdnj6XBDi332P1CGl7zyNho4aMhaQ== Received: from lists.sr.ht (unknown [46.23.81.154]) by mail-a.sr.ht (Postfix) with ESMTPSA id D28FA2025C for ; Fri, 8 Mar 2024 22:39:27 +0000 (UTC) Received: from iguana.tulip.relay.mailchannels.net (iguana.tulip.relay.mailchannels.net [23.83.218.253]) by mail-a.sr.ht (Postfix) with ESMTPS id D46822025C for <~johnnyrichard/olang-devel@lists.sr.ht>; Fri, 8 Mar 2024 22:39:26 +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 3A3D8902515 for <~johnnyrichard/olang-devel@lists.sr.ht>; Fri, 8 Mar 2024 22:39:25 +0000 (UTC) Received: from uk-fast-smtpout2.hostinger.io (unknown [127.0.0.6]) (Authenticated sender: hostingeremail) by relay.mailchannels.net (Postfix) with ESMTPA id 5DA8490250A for <~johnnyrichard/olang-devel@lists.sr.ht>; Fri, 8 Mar 2024 22:39:24 +0000 (UTC) ARC-Seal: i=1; s=arc-2022; d=mailchannels.net; t=1709937564; a=rsa-sha256; cv=none; b=B+ektseONL5MfgtHPVSXUPW2JPjtXwIxWB2Y98bqzDAkT8j9YMbYI366lF4eY9tSt2Mjx4 lQlf57JgxXCXJkOoIsmZ3SOjw5sOiVg/jHjsEvjn81dFhTNc4zM/pIjzSMQ6mc3bChlXvC 3J5GOAEXcMmXI29vCEGjxRilNMu/7VmRFpjef+kGBTrXK1t0u/4KUSojed4v3EzV0tLGCX RwJi2GFd3cZQNhGHZmGDoVLXh2kGxV1tIgFmrrBObmN+MZsoNxmdsFRtjaIstwr4AeQFAA p//wGbVZLx4wArEHOw/lW1zi8ci31vtXTqEWjfEOT1qMCKEv97T8xnxjFO13kg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=mailchannels.net; s=arc-2022; t=1709937564; 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=A8ZFrq1Rct97zut7NtvArxxNYnDDCnqP9JM2LNO/FNY=; b=wmxZ+O2vrW4wZ/+XInFHkkKpM9PgSdkD+OeZZtjBjE7cVeQRMn6nbjATuh60z9bWdN+zwb 0lbFpiXqdygJf1YnGjUFszZiJDfLBaKVjV78EcSb81NxbjrOeckbj7FJIybqFSpCmCTIvO douOEXpmh4buEf2SoFpq9Bc2+7cSF/ZN0vn6dLRpCBGWD0XG6s3Wm6ZhCfVutTiVpnqZym 39CIttdLU4Vimbxa6lnDL8dnEKv3yNRBclnXpOtCbsnqb33ft8bev/osu0N9qPJ71nnKSc NFpQNQ5i6Eqr4Q/hgz/9wd9ILD8JRymKRuonA9dg/+V8bNzbznmLLe4rrrYkkw== ARC-Authentication-Results: i=1; rspamd-67fb46fddd-r99b6; 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-Illustrious-Imminent: 554d79867d399378_1709937565103_2161515129 X-MC-Loop-Signature: 1709937565103:1547208938 X-MC-Ingress-Time: 1709937565103 Received: from uk-fast-smtpout2.hostinger.io (uk-fast-smtpout2.hostinger.io [31.220.23.36]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384) by 100.98.116.93 (trex/6.9.2); Fri, 08 Mar 2024 22:39:25 +0000 From: Carlos Maniero DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=maniero.me; s=hostingermail1; t=1709937563; 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=A8ZFrq1Rct97zut7NtvArxxNYnDDCnqP9JM2LNO/FNY=; b=WUtu3upcg+7PNHpiIZvLOYLOhS0VRQ9V4m/NPrumYUfNN6VyirDMOsCwnuBv91cEC+yi4C APYJQlNjSfO/RL6gxkgLP2wZbrgFGE8Cu1LcYGXzTxQvDbLGRNo3e3fnQ4Jp7hSkpmmXd6 ikaiaKIdbK5M1nrD8xUyyahAeL7JVsk42kc3AYY4lVxk1ohcft7GXbx93xPS/5MFZZI/IE lTpZ68qvWrgDdefrZrR+HPOO57yJXFYf9ZILAhSGNdYLuP5HL9hG7CvR7VGEmlcrTAUcK7 YB0gXzgBwf5xzXMBRHoBi1pQtQhN3trv/ZzlkW8rOKFV6yDSlYTaySmQjGLj9g== To: ~johnnyrichard/olang-devel@lists.sr.ht Cc: Carlos Maniero Subject: [PATCH olang v2 2/3] tests: decouple command execution from cli_runner Date: Fri, 8 Mar 2024 19:39:08 -0300 Message-Id: <20240308223909.3184226-3-carlos@maniero.me> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240308223909.3184226-1-carlos@maniero.me> References: <20240308223909.3184226-1-carlos@maniero.me> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-CM-Envelope: MS4xfHoiPMkRptKP325wk+RTdmtx24cibTXdSPkKriLH6CSFjZhQYbbdP9eh/CMfasZLA86ZPTbKfcydcGUzrOkzqg0A6sMTEj35wMVZDkYnL7b9Z3fSnnEM 05E3tEq12QqS38TlnmZpK74rKEBIJlzXySOBaPs87/dep+rYxs4VmKq4oQEkaCll1FCOlr2OQMTKMwecri8L0vT4z1z6ClqlHQmzNJfq1oxLuTTH04YlWmKZ X-CM-Analysis: v=2.4 cv=HawSTDE8 c=1 sm=1 tr=0 ts=65eb939a a=5+VMC1FZ3J4mVPAKpPmAqg==:117 a=5+VMC1FZ3J4mVPAKpPmAqg==:17 a=MKtGQD3n3ToA:10 a=1oJP67jkp3AA:10 a=mDV3o1hIAAAA:8 a=jV5FX1hQg6-HhU48JFMA:9 a=BXDaF_L80NY05PYiAFlV:22 a=_FVE-zBwftR9WsbkzFJk: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-Flow: FLOW_IN X-Migadu-Country: NL X-Migadu-Scanner: mx10.migadu.com X-Migadu-Spam-Score: -9.55 X-Spam-Score: -9.55 X-Migadu-Queue-Id: 126AF6A8C4 X-TUID: EKnvop2+Zw9x 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 --- V2: - Add a comment to stop truncating the stdout - Rename fd_pipe variable 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 | 63 ++++++++++++++++++++++++++++++++++ tests/integration/proc_exec.h | 36 +++++++++++++++++++ 6 files changed, 123 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..c22dd1e --- /dev/null +++ b/tests/integration/proc_exec.c @@ -0,0 +1,63 @@ +/* + * 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_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 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