-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtestEmbedMarkdownIncludes.sh
More file actions
executable file
·140 lines (106 loc) · 5.71 KB
/
testEmbedMarkdownIncludes.sh
File metadata and controls
executable file
·140 lines (106 loc) · 5.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#!/usr/bin/env bash
# Tests template processing for markdown by embedding includes.
# Requires embedMarkdownIncludes.sh
# Fail on any error ("-e" = exit on first error, "-o pipefail" exist on errors within piped commands)
set -o errexit -o pipefail
## Get this "scripts" directory if not already set
# Even if $BASH_SOURCE is made for Bourne-like shells it is also supported by others and therefore here the preferred solution.
# CDPATH reduces the scope of the cd command to potentially prevent unintended directory changes.
# This way non-standard tools like readlink aren't needed.
MARKDOWN_SCRIPTS_DIR=${MARKDOWN_SCRIPTS_DIR:-$( CDPATH=. cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd -P )} # Repository directory containing the shell scripts for markdown
echo "testEmbedMarkdownIncludes: MARKDOWN_SCRIPTS_DIR=${MARKDOWN_SCRIPTS_DIR}" >&2
tearDown() {
# echo "testEmbedMarkdownIncludes: Tear down tests...."
rm -rf "${temporaryTestDirectory}"
}
successful() {
local COLOR_SUCCESSFUL="\033[0;32m" # green
local COLOR_DEFAULT='\033[0m'
echo -e "testEmbedMarkdownIncludes: ${COLOR_SUCCESSFUL}Tests finished successfully.${COLOR_DEFAULT}"
tearDown
# If sourced, return to caller; if executed directly, exit.
if [ "${BASH_SOURCE[0]}" != "$0" ]; then
return 0
else
exit 0
fi
}
fail() {
local COLOR_ERROR='\033[0;31m' # red
local COLOR_DEFAULT='\033[0m'
local errorMessage="${1}"
echo -e "testEmbedMarkdownIncludes: ${COLOR_ERROR}${errorMessage}${COLOR_DEFAULT}"
tearDown
return 1
}
echo "testEmbedMarkdownIncludes: Starting tests...."
# Create testing resources
temporaryTestDirectory=$(mktemp -d 2>/dev/null || mktemp -d -t 'temporaryTestDirectory')
testMarkdownTemplate="${temporaryTestDirectory}/testMarkdownTemplate.md"
echo "<!-- include:testInclude.md -->" > "${testMarkdownTemplate}"
# Setup test files
mkdir -p "${temporaryTestDirectory}/includes"
# ------------------------------------------------------------
# Test case --
# ------------------------------------------------------------
echo "testEmbedMarkdownIncludes: 1.) An existing include file is correctly embedded."
# - Setup
testIncludeFile="includes/testInclude.md"
expected_test_include_content="This is the included content for the test."
echo "${expected_test_include_content}" > "${temporaryTestDirectory}/${testIncludeFile}"
# - Execute script under test
embeddedContent=$(cat "${testMarkdownTemplate}" | "${MARKDOWN_SCRIPTS_DIR}/embedMarkdownIncludes.sh" "${temporaryTestDirectory}/includes")
# - Verify results
if [ "${embeddedContent}" != "${expected_test_include_content}" ]; then
fail "1.) Test failed: Expected embedded content to be '${expected_test_include_content}', but got '${embeddedContent}'."
fi
# ------------------------------------------------------------
# Test case --
# ------------------------------------------------------------
echo "testEmbedMarkdownIncludes: 2.) An existing include file in the DEFAULT directory is correctly embedded."
# - Setup
testIncludeFile="includes/testInclude.md"
expected_test_include_content="This is the included content for the test."
echo "${expected_test_include_content}" > "${temporaryTestDirectory}/${testIncludeFile}"
# - Execute script under test
embeddedContent=$(cd "${temporaryTestDirectory}"; cat "${testMarkdownTemplate}" | "${MARKDOWN_SCRIPTS_DIR}/embedMarkdownIncludes.sh")
# - Verify results
if [ "${embeddedContent}" != "${expected_test_include_content}" ]; then
fail "2.) Test failed: Expected embedded content to be '${expected_test_include_content}', but got '${embeddedContent}'."
fi
# ------------------------------------------------------------
# Test case --
# ------------------------------------------------------------
echo "testEmbedMarkdownIncludes: 3.) A missing include file results in an error."
# - Setup
testMarkdownTemplateMissingInclude="${temporaryTestDirectory}/testMarkdownTemplateMissingInclude.md"
echo "<!-- include:nonExistentFile.md -->" > "${testMarkdownTemplateMissingInclude}"
# - Execute script under test
set +o errexit
errorOutput=$( { cat "${testMarkdownTemplateMissingInclude}" | "${MARKDOWN_SCRIPTS_DIR}/embedMarkdownIncludes.sh" "${temporaryTestDirectory}/includes" 2>&1 1>/dev/null; } )
exitCode=$?
set -o errexit
# - Verify results
if [ ${exitCode} -eq 0 ]; then
fail "2.) Test failed: Expected an error due to missing include file, but the script succeeded."
fi
if [[ "${errorOutput}" != *"ERROR: missing include file"* ]]; then
fail "2.) Test failed: Expected error message to contain 'ERROR: missing file', but got '${errorOutput}'."
fi
# ------------------------------------------------------------
# Test case --
# ------------------------------------------------------------
echo "testEmbedMarkdownIncludes: 4.) The fallback include is used when the main include is missing"
# - Setup
testFallbackIncludeFileName="testFallbackInclude.md"
echo "<!-- include:nonExistingInclude|${testFallbackIncludeFileName} -->" > "${testMarkdownTemplate}"
testFallbackIncludeFile="includes/${testFallbackIncludeFileName}"
expected_test_include_content="This is the included content from the fallback include."
echo "${expected_test_include_content}" > "${temporaryTestDirectory}/${testFallbackIncludeFile}"
# - Execute script under test
embeddedContent=$(cd "${temporaryTestDirectory}"; cat "${testMarkdownTemplate}" | "${MARKDOWN_SCRIPTS_DIR}/embedMarkdownIncludes.sh")
# - Verify results
if [ "${embeddedContent}" != "${expected_test_include_content}" ]; then
fail "4.) Test failed: Expected embedded content to be '${expected_test_include_content}', but got '${embeddedContent}'."
fi
successful