From patchwork Mon Mar 18 16:10:14 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 1057921 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=vger.kernel.org (client-ip=209.132.180.67; helo=vger.kernel.org; envelope-from=linux-gpio-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=bgdev.pl Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=bgdev-pl.20150623.gappssmtp.com header.i=@bgdev-pl.20150623.gappssmtp.com header.b="0wWK60/a"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 44NLkR4qB7z9sBV for ; Tue, 19 Mar 2019 03:10:23 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726897AbfCRQKV (ORCPT ); Mon, 18 Mar 2019 12:10:21 -0400 Received: from mail-wr1-f68.google.com ([209.85.221.68]:35058 "EHLO mail-wr1-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727142AbfCRQKV (ORCPT ); Mon, 18 Mar 2019 12:10:21 -0400 Received: by mail-wr1-f68.google.com with SMTP id w1so11773519wrp.2 for ; Mon, 18 Mar 2019 09:10:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=AM4QydSaZ1suQWU21WrzS6xQ9vpLBDbKHDYMzNQZo7s=; b=0wWK60/ac9evTF4ljmdewjvGrJinX+5r88z0CuvZAGzet6Es6UPctwBlF6TXDhRKMK 2vmmW0MoA6xhyMb/5tkTmui1V4WjAzkmXyPXXAQwDqJtYT6fAO/b6ukefH19GlveG3Ud 6EndxDmCOA0l+Oap/pBIjQELLqw8Hd6QeGk5G8h8w2xoI2L4IxciCixTSR8A7xadmekL Aut1/FoshGAO0vCV1TSC/Iy/FmNcJTHYUe72Z3zcWfvVd0tvFSC5rtqYpupYap7G6NAb rV8DO7CX2Dp0ZP4adnNH+tw6JJ+PedP9vpUlfJ4gChGuFIyQhyiayvWQY6ueHw/iY8E1 d1bA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=AM4QydSaZ1suQWU21WrzS6xQ9vpLBDbKHDYMzNQZo7s=; b=RxpqMqbnFh1kjfDBE1OHnPt4u3saO9o0iyo8Wf00lQixf76VFE91bZ/A8Q/dA9BR3Q miYQNroovFRIe3BX8+9bF39CVgXNfjHfy3LaG0CU1p66nFjY9HhYqQ3jR1BdODwkZ4re ipcXJZRRHxnsl+nMBaVN87vaLKVBhfDU/VZN9UD+LhxlzUDdwFDS1DYl/WWtJvbbgRmF AlYC+0vA+j6InUNEGIERSddOwLSPbUeqkFicf4AJlSf+cXxsykV2ZbVnsqXrMRacLTay Hfz6f25ebMR9yEvlI2D8zebz7H4qsFchNvCzs9SRjTBpn6NG4xWeI2tdaySkXAxvLh7K Orkg== X-Gm-Message-State: APjAAAWdrtv0HiAbCl5c5JJJfbKMfvQr+AoMyLfEe29fx3E+SEu0Qg3F Ikwo324A3sjp9DdDaDKZqShWZg== X-Google-Smtp-Source: APXvYqzUuU7dDjlqYFNumZ+72PvuO5G239PpGhhj8UL5IB68PuCdvksvDvsReJeJKgYIBYLn7HyQRg== X-Received: by 2002:adf:b651:: with SMTP id i17mr11981629wre.108.1552925419236; Mon, 18 Mar 2019 09:10:19 -0700 (PDT) Received: from debian-brgl.home ([2a01:cb1d:af:5b00:6d6c:8493:1ab5:dad7]) by smtp.gmail.com with ESMTPSA id q2sm23803401wrd.46.2019.03.18.09.10.18 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 18 Mar 2019 09:10:18 -0700 (PDT) From: Bartosz Golaszewski To: Anders Roxell Cc: linux-gpio@vger.kernel.org, Bartosz Golaszewski Subject: [libgpiod][PATCH v2 2/2] tests: look for gpio-tools in more places Date: Mon, 18 Mar 2019 17:10:14 +0100 Message-Id: <20190318161014.4660-3-brgl@bgdev.pl> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190318161014.4660-1-brgl@bgdev.pl> References: <20190318161014.4660-1-brgl@bgdev.pl> MIME-Version: 1.0 Sender: linux-gpio-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org From: Bartosz Golaszewski Currently we only support running the test cases for gpio-tools from the top-level source directory. Some users want to install the test executable and run the tests from other locations (/bin, /usr/bin or custom path). This patch makes the test suite look in the source tree path first, then check the directory in which the program resides and last iterate over all directories in $PATH. We only do that once at the beginning and then reuse the path later. Cc: Anders Roxell Signed-off-by: Bartosz Golaszewski --- tests/gpiod-test.c | 75 ++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 62 insertions(+), 13 deletions(-) diff --git a/tests/gpiod-test.c b/tests/gpiod-test.c index 4c51f4a..9cb9072 100644 --- a/tests/gpiod-test.c +++ b/tests/gpiod-test.c @@ -86,6 +86,7 @@ static struct { pid_t main_pid; int pipesize; char *pipebuf; + char *toolpath; } globals; enum { @@ -447,18 +448,6 @@ static void gpiotool_proc_dup_fds(int in_fd, int out_fd, int err_fd) } } -static char *gpiotool_proc_get_path(const char *tool) -{ - char *path, *progpath, *progdir; - - progpath = xstrdup(program_invocation_name); - progdir = dirname(progpath); - path = xappend(NULL, "%s/../../tools/%s", progdir, tool); - free(progpath); - - return path; -} - static NORETURN void gpiotool_proc_exec(const char *path, va_list va) { size_t num_args; @@ -545,10 +534,12 @@ void test_tool_run(char *tool, ...) event_lock(); if (globals.test_ctx.event.running) die("refusing to fork when the event thread is running"); + if (!globals.toolpath) + die("asked to run tests for gpio-tools, but the executables were not found"); gpiotool_proc_make_pipes(in_fds, out_fds, err_fds); - path = gpiotool_proc_get_path(tool); + path = xappend(NULL, "%s/%s", globals.toolpath, tool); rv = access(path, R_OK | X_OK); if (rv) die_perr("unable to execute '%s'", path); @@ -768,6 +759,63 @@ static void check_gpio_mockup(void) msg("gpio-mockup ok"); } +static void check_tool_path(void) +{ + /* + * Let's check gpiodetect only and assume all the other tools are in + * the same directory. + */ + static const char *const tool = "gpiodetect"; + + char *progpath, *progdir, *toolpath, *pathenv, *tok; + + /* First check if we're running the from the top source directory. */ + progpath = xstrdup(program_invocation_name); + progdir = dirname(progpath); + + toolpath = xappend(NULL, "%s/../../tools/%s", progdir, tool); + if (access(toolpath, R_OK | X_OK) == 0) { + free(progpath); + goto out; + } + free(toolpath); + + /* Is the tool in the same directory maybe? */ + toolpath = xappend(NULL, "%s/%s", progdir, tool); + free(progpath); + if (access(toolpath, R_OK | X_OK) == 0) + goto out; + free(toolpath); + + /* Next iterate over directories in $PATH. */ + pathenv = getenv("PATH"); + if (!pathenv) + return; + + progpath = xstrdup(pathenv); + tok = strtok(progpath, ":"); + while (tok) { + toolpath = xappend(NULL, "%s/%s", tok, tool); + if (access(toolpath, R_OK) == 0) { + free(progpath); + goto out; + } + + free(toolpath); + tok = strtok(NULL, ":"); + } + + free(progpath); + toolpath = NULL; + +out: + if (toolpath) { + toolpath = dirname(toolpath); + msg("using gpio-tools from '%s'", toolpath); + globals.toolpath = toolpath; + } +} + static void load_module(struct _test_chip_descr *descr) { unsigned int i; @@ -1001,6 +1049,7 @@ int main(int argc TEST_UNUSED, char **argv TEST_UNUSED) check_kernel(); check_gpio_mockup(); + check_tool_path(); msg("running tests");