From cfd13e8dda9d2db3f6bdf32d623aecf10ee1ba50 Mon Sep 17 00:00:00 2001
From: Stephen Warren <swarren@wwwdotorg.org>
Date: Sat, 1 Mar 2014 22:18:00 -0700
Subject: [PATCH] unit-test: make "test -e" test independent of $CWD

The unit-test for hush's "test -e" currently relies upon being run in
the U-Boot build directory, because it tests for the existence of a file
that exists in that directory.

Fix this by explicitly creating the file we use for the existence test,
and deleting it afterwards so that multiple successive unit-test
invocations succeed. This required adding an os.c function to erase
files.

Reported-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Stephen Warren <swarren@wwwdotorg.org>
---
 arch/sandbox/cpu/os.c |  5 +++++
 include/os.h          |  8 ++++++++
 test/command_ut.c     | 14 +++++++++-----
 3 files changed, 22 insertions(+), 5 deletions(-)

diff --git a/arch/sandbox/cpu/os.c b/arch/sandbox/cpu/os.c
index 2e2fc58a1b..98f565eaaf 100644
--- a/arch/sandbox/cpu/os.c
+++ b/arch/sandbox/cpu/os.c
@@ -92,6 +92,11 @@ int os_close(int fd)
 	return close(fd);
 }
 
+int os_unlink(const char *pathname)
+{
+	return unlink(pathname);
+}
+
 void os_exit(int exit_code)
 {
 	exit(exit_code);
diff --git a/include/os.h b/include/os.h
index d6d6e5794e..fa4e39fc74 100644
--- a/include/os.h
+++ b/include/os.h
@@ -83,6 +83,14 @@ int os_open(const char *pathname, int flags);
  */
 int os_close(int fd);
 
+/**
+ * Access to the OS unlink() system call
+ *
+ * \param pathname Path of file to delete
+ * \return 0 for success, other for error
+ */
+int os_unlink(const char *pathname);
+
 /**
  * Access to the OS exit() system call
  *
diff --git a/test/command_ut.c b/test/command_ut.c
index b6b6976616..aaa1ee25d0 100644
--- a/test/command_ut.c
+++ b/test/command_ut.c
@@ -7,6 +7,9 @@
 #define DEBUG
 
 #include <common.h>
+#ifdef CONFIG_SANDBOX
+#include <os.h>
+#endif
 
 static const char test_cmd[] = "setenv list 1\n setenv list ${list}2; "
 		"setenv list ${list}3\0"
@@ -156,11 +159,12 @@ static int do_ut_cmd(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
 	setenv("ut_var_test", NULL);
 
 #ifdef CONFIG_SANDBOX
-	/*
-	 * File existence
-	 * This assume U-Boot sandbox is run from the U-Boot build directory
-	 */
-	HUSH_TEST(e, "-e host - u-boot", y);
+	/* File existence */
+	HUSH_TEST(e, "-e host - creating_this_file_breaks_uboot_unit_test", n);
+	run_command("sb save host - creating_this_file_breaks_uboot_unit_test 0 1", 0);
+	HUSH_TEST(e, "-e host - creating_this_file_breaks_uboot_unit_test", y);
+	/* Perhaps this could be replaced by an "rm" shell command one day */
+	assert(!os_unlink("creating_this_file_breaks_uboot_unit_test"));
 	HUSH_TEST(e, "-e host - creating_this_file_breaks_uboot_unit_test", n);
 #endif
 #endif
-- 
2.39.5