t0028: extract encoding helpers to lib-encoding.sh

The following patch will add tests outside t0028 which will also need to re-encode some strings. Extract the auxiliary encoding functions from t0028 to a common lib file so that they can be reused. Signed-off-by: Matheus Tavares <matheus.bernardino@usp.br> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Matheus Tavares committed May 4, 2021 at 13:27 UTC 2fa3cbadcdc63c3a723c2fd490f1d84552854bee
2 files changed +26 -24
t/lib-encoding.sh new
+25
@@ -0,0 +1,25 @@
1 +# Encoding helpers
2 +
3 +test_lazy_prereq NO_UTF16_BOM '
4 + test $(printf abc | iconv -f UTF-8 -t UTF-16 | wc -c) = 6
5 +'
6 +
7 +test_lazy_prereq NO_UTF32_BOM '
8 + test $(printf abc | iconv -f UTF-8 -t UTF-32 | wc -c) = 12
9 +'
10 +
11 +write_utf16 () {
12 + if test_have_prereq NO_UTF16_BOM
13 + then
14 + printf '\376\377'
15 + fi &&
16 + iconv -f UTF-8 -t UTF-16
17 +}
18 +
19 +write_utf32 () {
20 + if test_have_prereq NO_UTF32_BOM
21 + then
22 + printf '\0\0\376\377'
23 + fi &&
24 + iconv -f UTF-8 -t UTF-32
25 +}
t/t0028-working-tree-encoding.sh
+1 -24
@@ -6,33 +6,10 @@ GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
6 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
7
8 . ./test-lib.sh
9 +. "$TEST_DIRECTORY/lib-encoding.sh"
10
11 GIT_TRACE_WORKING_TREE_ENCODING=1 && export GIT_TRACE_WORKING_TREE_ENCODING
12
12 -test_lazy_prereq NO_UTF16_BOM '
13 - test $(printf abc | iconv -f UTF-8 -t UTF-16 | wc -c) = 6
14 -'
15 -
16 -test_lazy_prereq NO_UTF32_BOM '
17 - test $(printf abc | iconv -f UTF-8 -t UTF-32 | wc -c) = 12
18 -'
19 -
20 -write_utf16 () {
21 - if test_have_prereq NO_UTF16_BOM
22 - then
23 - printf '\376\377'
24 - fi &&
25 - iconv -f UTF-8 -t UTF-16
26 -}
27 -
28 -write_utf32 () {
29 - if test_have_prereq NO_UTF32_BOM
30 - then
31 - printf '\0\0\376\377'
32 - fi &&
33 - iconv -f UTF-8 -t UTF-32
34 -}
35 -
13 test_expect_success 'setup test files' '
14 git config core.eol lf &&
15