소스 검색

split tests and ensure test fails when sudo output is redirected to pseudo tty

Pi 2 달 전
부모
커밋
03ac40eb51
1개의 변경된 파일47개의 추가작업 그리고 7개의 파일을 삭제
  1. 47 7
      tests/test.sh

+ 47 - 7
tests/test.sh

@@ -9,7 +9,10 @@ then
 	REDIR1=/proc/self/fd/1
 	REDIR2=/proc/self/fd/2
 else
-	REDIR1=/dev/null
+	# can not redirect stdout
+	# as behaviour is altered
+	# if stdout is tty or not
+	REDIR1=/proc/self/fd/1
 	REDIR2=/dev/null
 fi
 DEFAULT_CLEANUP=false
@@ -25,6 +28,7 @@ TEST_CASE () { {
 		printf "\x1b[s\x1b[1F\x1b[2CSKIPPED  \x1b[u" >&3
 		exit
 	fi
+	find "$LOG_ROOT" -mindepth 1 -delete
 	: "$@"
 } 2>"$REDIR2"; }
 
@@ -59,28 +63,64 @@ export RECALL_FZF_ENABLE RECALL_VIPE_ENABLE RECALL_ISELECT_ENABLE
 : "CLEANUP=$CLEANUP"
 : "LOG_ROOT=$LOG_ROOT"
 
-( TEST_CASE "Capture : ls"
-
+( TEST_CASE "Capture : subshell : ls"
+	: Given
 	CMD=(ls -lah capture-ls-data)
+	: When
 	OUT="$(+ "${CMD[@]}")"
+	: Then
 	[ "$OUT" = "$("${CMD[@]}")" ]
 	@ sudo ls && exit 2
 	[ "$(@ ls)" = "$("${CMD[@]}")" ]
 	@ -ln1 sudo ls && exit 2
-	[ "$(@ -ln1 ls)" = "$(readlink -e "$LOG_ROOT"/ls/*)" ]
+	[ "$(@ -ln1 ls)" = "$(find "$LOG_ROOT"/ls -mindepth 1 -maxdepth 1 -print0 | head -z -n1 | xargs -0 readlink -e)" ]
 	@ -sln1 sudo ls && exit 2
 	[ "$(@ -sln1 ls)" = "${CMD[*]}" ]
 ) >"$REDIR1"
 
-( TEST_CASE "Capture : sudo ls"
+( TEST_CASE "Capture : ls"
+	: Given
+	CMD=(ls -lah capture-ls-data)
+	: When
+	+ "${CMD[@]}"
+	: Then
+	@ sudo ls && exit 2
+	[ "$(@ ls)" = "$("${CMD[@]}")" ]
+	@ -ln1 sudo ls && exit 2
+	[ "$(@ -ln1 ls)" = "$(find "$LOG_ROOT"/ls -mindepth 1 -maxdepth 1 -print0 | head -z -n1 | xargs -0 readlink -e)" ]
+	@ -sln1 sudo ls && exit 2
+	[ "$(@ -sln1 ls)" = "${CMD[*]}" ]
+) >"$REDIR1"
 
+( TEST_CASE "Capture : subshell : ls; sudo ls"
+	: Given
+	CMD=(ls -lah capture-ls-data)
+	+ "${CMD[@]}"
 	CMD=(sudo ls -lah capture-ls-data)
+	: When
 	OUT="$(+ "${CMD[@]}")"
+	: Then
 	[ "$OUT" = "$("${CMD[@]}")" ]
 	[ "$(@ sudo ls)" = "$("${CMD[@]}")" ]
 	[ "$(@ ls)" = "$("${CMD[@]}")" ]
-	[ "$(@ -ln1 sudo ls)" = "$(readlink -e "$LOG_ROOT"/sudo/ls/*)" ]
-	[ "$(@ -ln1 ls)" = "$(readlink -e "$LOG_ROOT"/sudo/ls/*)" ]
+	[ "$(@ -ln1 sudo ls)" = "$(find "$LOG_ROOT"/sudo/ls -mindepth 1 -maxdepth 1 -print0 | head -z -n1 | xargs -0 readlink -e)" ]
+	[ "$(@ -ln1 ls)" = "$(find "$LOG_ROOT"/sudo/ls -mindepth 1 -maxdepth 1 -print0 | head -z -n1 | xargs -0 readlink -e)" ]
+	[ "$(@ -sln1 sudo ls)" = "${CMD[*]}" ]
+	[ "$(@ -sln1 ls)" = "${CMD[*]}" ]
+) >"$REDIR1"
+
+( TEST_CASE "Capture : ls; sudo ls"
+	: Given
+	CMD=(ls -lah capture-ls-data)
+	+ "${CMD[@]}"
+	CMD=(sudo ls -lah capture-ls-data)
+	: When
+	+ "${CMD[@]}"
+	: Then
+	[ "$(@ sudo ls)" = "$("${CMD[@]}")" ]
+	[ "$(@ ls)" = "$("${CMD[@]}")" ]
+	[ "$(@ -ln1 sudo ls)" = "$(find "$LOG_ROOT"/sudo/ls -mindepth 1 -maxdepth 1 -print0 | head -z -n1 | xargs -0 readlink -e)" ]
+	[ "$(@ -ln1 ls)" = "$(find "$LOG_ROOT"/sudo/ls -mindepth 1 -maxdepth 1 -print0 | head -z -n1 | xargs -0 readlink -e)" ]
 	[ "$(@ -sln1 sudo ls)" = "${CMD[*]}" ]
 	[ "$(@ -sln1 ls)" = "${CMD[*]}" ]
 ) >"$REDIR1"