git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v11 02/15] test: test relative_path through test-path-utils

From
Jiang Xin <worldhello.net@gmail.com>
Date
May 17, 2013, 09:20 UTC
Message-ID
<c856c65a9f8b28cdead5cc89c182ed2a2a97c9a5.1368782129.git.worldhello.net@gmail.com>
In-Reply-To
<cover.1368782129.git.worldhello.net@gmail.com>

Add subcommand "relative_path" in test-path-utils, and add test cases in t0060.

Signed-off-by: Jiang Xin <worldhello.net@gmail.com>
---
 t/t0060-path-utils.sh | 23 +++++++++++++++++++++++
 test-path-utils.c     | 24 ++++++++++++++++++++++++
 2 files changed, 47 insertions(+)
diff --git a/t/t0060-path-utils.sh b/t/t0060-path-utils.sh
index 09a42..a51d7 100755
--- a/t/t0060-path-utils.sh
+++ b/t/t0060-path-utils.sh
@@ -12,6 +12,11 @@ norm_path() {
 	"test \"\$(test-path-utils normalize_path_copy '$1')\" = '$2'"
 }
 
+relative_path() {
+	test_expect_success $4 "relative path: $1 $2 => $3" \
+	"test \"\$(test-path-utils relative_path '$1' '$2')\" = '$3'"
+}
+
 # On Windows, we are using MSYS's bash, which mangles the paths.
 # Absolute paths are anchored at the MSYS installation directory,
 # which means that the path / accounts for this many characters:
@@ -183,4 +188,22 @@ test_expect_success SYMLINKS 'real path works on symlinks' '
 	test "$sym" = "$(test-path-utils real_path "$dir2/syml")"
 '
 
+relative_path /a/b/c/	/a/b/		c/
+relative_path /a/b/c/	/a/b		c/
+relative_path /a//b//c/	//a/b//		c/
+relative_path /a/b	/a/b		./
+relative_path /a/b/	/a/b		./
+relative_path /a	/a/b		../
+relative_path /		/a/b/		../../
+relative_path /a/c	/a/b/		../c
+relative_path /a/c	/a/b		../c
+relative_path /a/b	"(empty)"	/a/b
+relative_path /a/b 	"(null)"	/a/b
+relative_path "(empty)"	/a/b		./
+relative_path "(empty)"	"(empty)"	./
+relative_path "(empty)"	"(null)"	./
+relative_path "(null)"	/a/b		./
+relative_path "(null)"	"(empty)"	./
+relative_path "(null)"	"(null)"	./
+
 test_done
diff --git a/test-path-utils.c b/test-path-utils.c
index 0092cb..d759c 100644
--- a/test-path-utils.c
+++ b/test-path-utils.c
@@ -1,6 +1,19 @@
 #include "cache.h"
 #include "string-list.h"
 
+#define PARSE_ARGV_STRING(var, input) do { \
+	if (!strcmp(input, "(null)")) { \
+		var = NULL; \
+	} else if (!strcmp(input, "(empty)")) { \
+		var = ""; \
+	} else if (*input == '(') { \
+		fprintf(stderr, "Bad value: %s\n", input); \
+		return 1; \
+	} else { \
+		var = input; \
+	} \
+} while (0)
+
 /*
  * A "string_list_each_func_t" function that normalizes an entry from
  * GIT_CEILING_DIRECTORIES.  If the path is unusable for some reason,
@@ -103,6 +116,17 @@ int main(int argc, char **argv)
 		return 0;
 	}
 
+	if (argc == 4 && !strcmp(argv[1], "relative_path")) {
+		struct strbuf sb = STRBUF_INIT;
+		const char *abs, *base, *rel;
+		PARSE_ARGV_STRING(abs, argv[2]);
+		PARSE_ARGV_STRING(base, argv[3]);
+		rel = relative_path(abs, base, &sb);
+		puts(rel ? rel : "(null)");
+		strbuf_release(&sb);
+		return 0;
+	}
+
 	fprintf(stderr, "%s: unknown function name: %s\n", argv[0],
 		argv[1] ? argv[1] : "(there was none)");
 	return 1;
-- 
1.8.3.rc2.380.g956c2b2
Previous: Jiang XinNext: Jiang Xin
Message 3 of 16 in “*** Interactive git-clean ***”
  1. 00/15 *** Interactive git-clean ***Jiang Xin, May 17, 2013
  2. 01/15 path.c: refactor relative_path(), not only strip prefixJiang Xin, May 17, 2013
  3. 02/15 test: test relative_path through test-path-utilsJiang Xin, May 17, 2013
  4. 03/15 quote.c: remove path_relative, use relative_path insteadJiang Xin, May 17, 2013
  5. 04/15 Refactor quote_path_relative, remove unused paramsJiang Xin, May 17, 2013
  6. 05/15 Refactor write_name_quoted_relative, remove unused paramsJiang Xin, May 17, 2013
  7. 06/15 git-clean: refactor git-clean into two phasesJiang Xin, May 17, 2013
  8. 07/15 git-clean: add support for -i/--interactiveJiang Xin, May 17, 2013
  9. 08/15 git-clean: show items of del_list in columnsJiang Xin, May 17, 2013
  10. 09/15 git-clean: add colors to interactive git-cleanJiang Xin, May 17, 2013
  11. 10/15 git-clean: use a git-add-interactive compatible UIJiang Xin, May 17, 2013
  12. 11/15 git-clean: add filter by pattern interactive actionJiang Xin, May 17, 2013
  13. 12/15 git-clean: add select by numbers interactive actionJiang Xin, May 17, 2013
  14. 13/15 git-clean: add ask each interactive actionJiang Xin, May 17, 2013
  15. 14/15 git-clean: add documentation for interactive git-cleanJiang Xin, May 17, 2013
  16. 15/15 test: add t7301 for git-clean--interactiveJiang Xin, May 17, 2013

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.