From 1b26ca724f9dda116ab959db14e5bafc64bbf51b Mon Sep 17 00:00:00 2001
From: rht <rhtbot@gmail.com>
Date: Fri, 4 Sep 2015 16:55:34 +0700
Subject: [PATCH] Add test for ipfs add err for unsupported file type

License: MIT
Signed-off-by: rht <rhtbot@gmail.com>
---
 test/sharness/lib/test-lib.sh          | 14 ++++++++++++++
 test/sharness/t0040-add-and-cat.sh     | 19 +++++++++++++++++++
 test/sharness/t0041-add-cat-offline.sh | 19 +++++++++++++++++++
 3 files changed, 52 insertions(+)

diff --git a/test/sharness/lib/test-lib.sh b/test/sharness/lib/test-lib.sh
index 78c358b14..1d6866c48 100644
--- a/test/sharness/lib/test-lib.sh
+++ b/test/sharness/lib/test-lib.sh
@@ -330,3 +330,17 @@ disk_usage() {
     esac
         $DU "$1" | awk "{print \$1}"
 }
+
+# output a file's permission in human readable format
+generic_stat() {
+    # normalize stat across systems
+    case $(uname -s) in
+        Linux)
+            _STAT="stat -c %A"
+            ;;
+        FreeBSD | Darwin | DragonFly)
+            _STAT="stat -f %Sp"
+            ;;
+    esac
+    $_STAT "$1"
+}
diff --git a/test/sharness/t0040-add-and-cat.sh b/test/sharness/t0040-add-and-cat.sh
index 28da0225c..85739a7b5 100755
--- a/test/sharness/t0040-add-and-cat.sh
+++ b/test/sharness/t0040-add-and-cat.sh
@@ -8,6 +8,10 @@ test_description="Test add and cat commands"
 
 . lib/test-lib.sh
 
+client_err() {
+    printf "$@\n\nUse 'ipfs add --help' for information about this command\n"
+}
+
 test_launch_ipfs_daemon_and_mount
 
 test_expect_success "'ipfs add --help' succeeds" '
@@ -262,6 +266,21 @@ test_expect_success FUSE,EXPENSIVE "cat ipfs/bigfile looks good" '
 	test_cmp sha1_expected sha1_actual
 '
 
+test_expect_success "useful error message when adding a named pipe" '
+	mkfifo named-pipe &&
+	test_expect_code 1 ipfs add named-pipe 2>actual &&
+    client_err "Error: Unrecognized file type for named-pipe: $(generic_stat named-pipe)" >expected &&
+	test_cmp expected actual
+'
+
+test_expect_success "useful error message when recursively adding a named pipe" '
+	mkdir named-pipe-dir &&
+	mkfifo named-pipe-dir/named-pipe &&
+	test_expect_code 1 ipfs add -r named-pipe-dir 2>actual &&
+    printf "Error: Post http://127.0.0.1:$PORT_API/api/v0/add?encoding=json&progress=true&r=true&stream-channels=true: Unrecognized file type for named-pipe-dir/named-pipe: $(generic_stat named-pipe-dir/named-pipe)\n" >expected &&
+	test_cmp expected actual
+'
+
 test_kill_ipfs_daemon
 
 test_done
diff --git a/test/sharness/t0041-add-cat-offline.sh b/test/sharness/t0041-add-cat-offline.sh
index 68f632267..96c61afe9 100755
--- a/test/sharness/t0041-add-cat-offline.sh
+++ b/test/sharness/t0041-add-cat-offline.sh
@@ -8,6 +8,10 @@ test_description="Test add and cat commands"
 
 . lib/test-lib.sh
 
+client_err() {
+    printf "$@\n\nUse 'ipfs add --help' for information about this command\n"
+}
+
 test_init_ipfs
 
 test_expect_success "ipfs add file succeeds" '
@@ -53,4 +57,19 @@ test_expect_success "ipfs cat file fails" '
 	test_must_fail ipfs cat $(cat oh_hash)
 '
 
+test_expect_success "useful error message when adding a named pipe" '
+	mkfifo named-pipe &&
+	test_expect_code 1 ipfs add named-pipe 2>actual &&
+    client_err "Error: Unrecognized file type for named-pipe: $(generic_stat named-pipe)" >expected &&
+	test_cmp expected actual
+'
+
+test_expect_success "useful error message when recursively adding a named pipe" '
+	mkdir named-pipe-dir &&
+	mkfifo named-pipe-dir/named-pipe &&
+	test_expect_code 1 ipfs add -r named-pipe-dir 2>actual &&
+    printf "Error: Unrecognized file type for named-pipe-dir/named-pipe: $(generic_stat named-pipe-dir/named-pipe)\n" >expected &&
+	test_cmp expected actual
+'
+
 test_done