unofficial mirror of notmuch@notmuchmail.org
 help / color / mirror / code / Atom feed
* [PATCH 1/3] test: initial tests for external commands
@ 2022-05-31 11:50 David Bremner
  2022-05-31 11:50 ` [PATCH 2/3] CLI: pass --config to external commands via NOTMUCH_CONFIG David Bremner
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: David Bremner @ 2022-05-31 11:50 UTC (permalink / raw)
  To: notmuch

The main goal is to test the setting of NOTMUCH_CONFIG, but also
include a basic sanity test for execing scripts.
---
 test/T405-external.sh | 55 +++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 55 insertions(+)
 create mode 100755 test/T405-external.sh

diff --git a/test/T405-external.sh b/test/T405-external.sh
new file mode 100755
index 00000000..b121fc72
--- /dev/null
+++ b/test/T405-external.sh
@@ -0,0 +1,55 @@
+#!/usr/bin/env bash
+test_description='hooks'
+. $(dirname "$0")/test-lib.sh || exit 1
+
+create_echo_script () {
+    local TOKEN="${RANDOM}"
+    mkdir -p ${BIN_DIR}
+    cat <<EOF >"${BIN_DIR}/${1}"
+#!/bin/sh
+echo "${TOKEN}" > ${3}
+EOF
+    chmod +x "${BIN_DIR}/${1}"
+    echo "${TOKEN}" > ${2}
+}
+
+create_printenv_script () {
+    mkdir -p ${BIN_DIR}
+    cat <<EOF >"${BIN_DIR}/${1}"
+#!/bin/sh
+printenv "${2}" > "${3}"
+EOF
+    chmod +x "${BIN_DIR}/${1}"
+}
+
+# add a message to generate mail dir and database
+add_message
+
+BIN_DIR=`pwd`/bin
+PATH=$BIN_DIR:$PATH
+
+test_begin_subtest "'notmuch foo' runs notmuch-foo"
+rm -rf ${BIN_DIR}
+create_echo_script "notmuch-foo" EXPECTED OUTPUT $HOOK_DIR
+notmuch foo
+test_expect_equal_file_nonempty EXPECTED OUTPUT
+
+create_printenv_script "notmuch-printenv" NOTMUCH_CONFIG OUTPUT
+
+test_begin_subtest "NOTMUCH_CONFIG is set"
+notmuch printenv
+cat <<EOF > EXPECTED
+${NOTMUCH_CONFIG}
+EOF
+test_expect_equal_file_nonempty EXPECTED OUTPUT
+
+test_begin_subtest "NOTMUCH_CONFIG is set by --config"
+test_subtest_known_broken
+cp "${NOTMUCH_CONFIG}" "${NOTMUCH_CONFIG}.alternate"
+cat <<EOF > EXPECTED
+${NOTMUCH_CONFIG}.alternate
+EOF
+notmuch --config "${NOTMUCH_CONFIG}.alternate" printenv
+test_expect_equal_file_nonempty EXPECTED OUTPUT
+
+test_done
-- 
2.35.2

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH 2/3] CLI: pass --config to external commands via NOTMUCH_CONFIG.
  2022-05-31 11:50 [PATCH 1/3] test: initial tests for external commands David Bremner
@ 2022-05-31 11:50 ` David Bremner
  2022-05-31 11:51 ` [PATCH 3/3] CLI: document handling of --config for external commands David Bremner
  2022-06-13 10:35 ` [PATCH 1/3] test: initial tests " David Bremner
  2 siblings, 0 replies; 4+ messages in thread
From: David Bremner @ 2022-05-31 11:50 UTC (permalink / raw)
  To: notmuch

This makes `notmuch --config foo external-subcommand` work
consistently with the built in subcommands.
---
 notmuch.c             | 11 +++++++++--
 test/T405-external.sh |  1 -
 2 files changed, 9 insertions(+), 3 deletions(-)

diff --git a/notmuch.c b/notmuch.c
index ac25ae18..8398501f 100644
--- a/notmuch.c
+++ b/notmuch.c
@@ -443,11 +443,18 @@ notmuch_command (notmuch_database_t *notmuch,
  * false on errors.
  */
 static bool
-try_external_command (char *argv[])
+try_external_command (const char *config_file_name, char *argv[])
 {
     char *old_argv0 = argv[0];
     bool ret = true;
 
+    if (config_file_name) {
+	if (setenv ("NOTMUCH_CONFIG", config_file_name, 1)) {
+	    perror ("setenv");
+	    exit (1);
+	}
+    }
+
     argv[0] = talloc_asprintf (NULL, "notmuch-%s", old_argv0);
 
     /*
@@ -507,7 +514,7 @@ main (int argc, char *argv[])
     /* if command->function is NULL, try external command */
     if (! command || ! command->function) {
 	/* This won't return if the external command is found. */
-	if (try_external_command (argv + opt_index))
+	if (try_external_command (config_file_name, argv + opt_index))
 	    fprintf (stderr, "Error: Unknown command '%s' (see \"notmuch help\")\n",
 		     command_name);
 	ret = EXIT_FAILURE;
diff --git a/test/T405-external.sh b/test/T405-external.sh
index b121fc72..0e1d9646 100755
--- a/test/T405-external.sh
+++ b/test/T405-external.sh
@@ -44,7 +44,6 @@ EOF
 test_expect_equal_file_nonempty EXPECTED OUTPUT
 
 test_begin_subtest "NOTMUCH_CONFIG is set by --config"
-test_subtest_known_broken
 cp "${NOTMUCH_CONFIG}" "${NOTMUCH_CONFIG}.alternate"
 cat <<EOF > EXPECTED
 ${NOTMUCH_CONFIG}.alternate
-- 
2.35.2

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH 3/3] CLI: document handling of --config for external commands
  2022-05-31 11:50 [PATCH 1/3] test: initial tests for external commands David Bremner
  2022-05-31 11:50 ` [PATCH 2/3] CLI: pass --config to external commands via NOTMUCH_CONFIG David Bremner
@ 2022-05-31 11:51 ` David Bremner
  2022-06-13 10:35 ` [PATCH 1/3] test: initial tests " David Bremner
  2 siblings, 0 replies; 4+ messages in thread
From: David Bremner @ 2022-05-31 11:51 UTC (permalink / raw)
  To: notmuch

Create a section of the notmuch(1) manpage to have some place to put
this documentation.
---
 doc/man1/notmuch.rst | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/doc/man1/notmuch.rst b/doc/man1/notmuch.rst
index c6ee3036..e0892e01 100644
--- a/doc/man1/notmuch.rst
+++ b/doc/man1/notmuch.rst
@@ -185,6 +185,14 @@ of notmuch.
    If set to a non-empty value, the notmuch library will print (to
    stderr) Xapian queries it constructs.
 
+EXTERNAL COMMANDS
+=================
+
+When given a subcommand `foo` not recognized as built-in, notmuch will
+try to run the command (e.g. script) `notmuch-foo` in the user's
+path. The :envvar:`NOTMUCH_CONFIG` will be set according to
+:option:`--config`, if the latter is present.
+
 SEE ALSO
 ========
 
-- 
2.35.2

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH 1/3] test: initial tests for external commands
  2022-05-31 11:50 [PATCH 1/3] test: initial tests for external commands David Bremner
  2022-05-31 11:50 ` [PATCH 2/3] CLI: pass --config to external commands via NOTMUCH_CONFIG David Bremner
  2022-05-31 11:51 ` [PATCH 3/3] CLI: document handling of --config for external commands David Bremner
@ 2022-06-13 10:35 ` David Bremner
  2 siblings, 0 replies; 4+ messages in thread
From: David Bremner @ 2022-06-13 10:35 UTC (permalink / raw)
  To: notmuch

David Bremner <david@tethera.net> writes:

> The main goal is to test the setting of NOTMUCH_CONFIG, but also
> include a basic sanity test for execing scripts.

Series applied to master

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2022-06-13 10:35 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-05-31 11:50 [PATCH 1/3] test: initial tests for external commands David Bremner
2022-05-31 11:50 ` [PATCH 2/3] CLI: pass --config to external commands via NOTMUCH_CONFIG David Bremner
2022-05-31 11:51 ` [PATCH 3/3] CLI: document handling of --config for external commands David Bremner
2022-06-13 10:35 ` [PATCH 1/3] test: initial tests " David Bremner

Code repositories for project(s) associated with this inbox:

	notmuch.git.git (no URL configured)

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).