diff mbox series

[libgpiod,2/2] tests: look for gpio-tools in more places

Message ID 20190315142353.17264-3-brgl@bgdev.pl
State New
Headers show
Series tests: support installing the test executable | expand

Commit Message

Bartosz Golaszewski March 15, 2019, 2:23 p.m. UTC
From: Bartosz Golaszewski <bgolaszewski@baylibre.com>

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.

Cc: Anders Roxell <anders.roxell@linaro.org>
Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com>
---
 tests/gpiod-test.c | 40 +++++++++++++++++++++++++++++++++++++---
 1 file changed, 37 insertions(+), 3 deletions(-)

Comments

Anders Roxell March 16, 2019, 8:38 a.m. UTC | #1
On 2019-03-15 15:23, Bartosz Golaszewski wrote:
> From: Bartosz Golaszewski <bgolaszewski@baylibre.com>
> 
> 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.
> 
> Cc: Anders Roxell <anders.roxell@linaro.org>
> Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com>

Reviewed-and-Tested-by: Anders Roxell <anders.roxell@linaro.org>

Cheers,
Anders

> ---
>  tests/gpiod-test.c | 40 +++++++++++++++++++++++++++++++++++++---
>  1 file changed, 37 insertions(+), 3 deletions(-)
> 
> diff --git a/tests/gpiod-test.c b/tests/gpiod-test.c
> index 4c51f4a..7c7b54c 100644
> --- a/tests/gpiod-test.c
> +++ b/tests/gpiod-test.c
> @@ -449,14 +449,48 @@ 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;
> +	char *progpath, *progdir, *toolpath, *pathenv, *tok;
>  
> +	/*
> +	 * First check if we're running the tool from the top source
> +	 * directory.
> +	 */
>  	progpath = xstrdup(program_invocation_name);
>  	progdir = dirname(progpath);
> -	path = xappend(NULL, "%s/../../tools/%s", progdir, tool);
> +
> +	toolpath = xappend(NULL, "%s/../../tools/%s", progdir, tool);
> +	if (access(toolpath, R_OK | X_OK) == 0)
> +		goto out;
> +	free(toolpath);
> +
> +	/* Is the tool in the same directory maybe? */
> +	toolpath = xappend(NULL, "%s/%s", progdir, tool);
> +	if (access(toolpath, R_OK | X_OK) == 0)
> +		goto out;
> +	free(toolpath);
>  	free(progpath);
>  
> -	return path;
> +	/* Next iterate over directories in $PATH. */
> +	pathenv = getenv("PATH");
> +	if (!pathenv)
> +		return NULL;
> +
> +	progpath = xstrdup(pathenv);
> +	tok = strtok(progpath, ":");
> +	while (tok) {
> +		toolpath = xappend(NULL, "%s/%s", tok, tool);
> +		if (access(toolpath, R_OK) == 0)
> +			goto out;
> +
> +		free(toolpath);
> +		tok = strtok(NULL, ":");
> +	}
> +
> +	toolpath = NULL;
> +
> +out:
> +	free(progpath);
> +	return toolpath;
>  }
>  
>  static NORETURN void gpiotool_proc_exec(const char *path, va_list va)
> -- 
> 2.20.1
>
Bartosz Golaszewski March 18, 2019, 10:10 a.m. UTC | #2
sob., 16 mar 2019 o 10:22 Anders Roxell <anders.roxell@linaro.org> napisaƂ(a):
>
> On 2019-03-15 15:23, Bartosz Golaszewski wrote:
> > From: Bartosz Golaszewski <bgolaszewski@baylibre.com>
> >
> > 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.
> >
> > Cc: Anders Roxell <anders.roxell@linaro.org>
> > Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com>
>
> Reviewed-and-Tested-by: Anders Roxell <anders.roxell@linaro.org>
>

I'll modify this even more - I think it's best to check the path once
in the beginning and simply store it for later tests.

Bart

> Cheers,
> Anders
>
> > ---
> >  tests/gpiod-test.c | 40 +++++++++++++++++++++++++++++++++++++---
> >  1 file changed, 37 insertions(+), 3 deletions(-)
> >
> > diff --git a/tests/gpiod-test.c b/tests/gpiod-test.c
> > index 4c51f4a..7c7b54c 100644
> > --- a/tests/gpiod-test.c
> > +++ b/tests/gpiod-test.c
> > @@ -449,14 +449,48 @@ 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;
> > +     char *progpath, *progdir, *toolpath, *pathenv, *tok;
> >
> > +     /*
> > +      * First check if we're running the tool from the top source
> > +      * directory.
> > +      */
> >       progpath = xstrdup(program_invocation_name);
> >       progdir = dirname(progpath);
> > -     path = xappend(NULL, "%s/../../tools/%s", progdir, tool);
> > +
> > +     toolpath = xappend(NULL, "%s/../../tools/%s", progdir, tool);
> > +     if (access(toolpath, R_OK | X_OK) == 0)
> > +             goto out;
> > +     free(toolpath);
> > +
> > +     /* Is the tool in the same directory maybe? */
> > +     toolpath = xappend(NULL, "%s/%s", progdir, tool);
> > +     if (access(toolpath, R_OK | X_OK) == 0)
> > +             goto out;
> > +     free(toolpath);
> >       free(progpath);
> >
> > -     return path;
> > +     /* Next iterate over directories in $PATH. */
> > +     pathenv = getenv("PATH");
> > +     if (!pathenv)
> > +             return NULL;
> > +
> > +     progpath = xstrdup(pathenv);
> > +     tok = strtok(progpath, ":");
> > +     while (tok) {
> > +             toolpath = xappend(NULL, "%s/%s", tok, tool);
> > +             if (access(toolpath, R_OK) == 0)
> > +                     goto out;
> > +
> > +             free(toolpath);
> > +             tok = strtok(NULL, ":");
> > +     }
> > +
> > +     toolpath = NULL;
> > +
> > +out:
> > +     free(progpath);
> > +     return toolpath;
> >  }
> >
> >  static NORETURN void gpiotool_proc_exec(const char *path, va_list va)
> > --
> > 2.20.1
> >
diff mbox series

Patch

diff --git a/tests/gpiod-test.c b/tests/gpiod-test.c
index 4c51f4a..7c7b54c 100644
--- a/tests/gpiod-test.c
+++ b/tests/gpiod-test.c
@@ -449,14 +449,48 @@  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;
+	char *progpath, *progdir, *toolpath, *pathenv, *tok;
 
+	/*
+	 * First check if we're running the tool from the top source
+	 * directory.
+	 */
 	progpath = xstrdup(program_invocation_name);
 	progdir = dirname(progpath);
-	path = xappend(NULL, "%s/../../tools/%s", progdir, tool);
+
+	toolpath = xappend(NULL, "%s/../../tools/%s", progdir, tool);
+	if (access(toolpath, R_OK | X_OK) == 0)
+		goto out;
+	free(toolpath);
+
+	/* Is the tool in the same directory maybe? */
+	toolpath = xappend(NULL, "%s/%s", progdir, tool);
+	if (access(toolpath, R_OK | X_OK) == 0)
+		goto out;
+	free(toolpath);
 	free(progpath);
 
-	return path;
+	/* Next iterate over directories in $PATH. */
+	pathenv = getenv("PATH");
+	if (!pathenv)
+		return NULL;
+
+	progpath = xstrdup(pathenv);
+	tok = strtok(progpath, ":");
+	while (tok) {
+		toolpath = xappend(NULL, "%s/%s", tok, tool);
+		if (access(toolpath, R_OK) == 0)
+			goto out;
+
+		free(toolpath);
+		tok = strtok(NULL, ":");
+	}
+
+	toolpath = NULL;
+
+out:
+	free(progpath);
+	return toolpath;
 }
 
 static NORETURN void gpiotool_proc_exec(const char *path, va_list va)