aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBen Hilburn <bhilburn@gmail.com>2017-03-21 04:57:36 +0300
committerGitHub <noreply@github.com>2017-03-21 04:57:36 +0300
commit0132c5dc35eefa428e0b914c724c28c8068ac7e1 (patch)
treec431e6e1226b70760311af62bb518919dbaaef55
parent33b41ced8057ac5045e848bfa01c797d55a0b24e (diff)
parent56af9bb3c3af7fde10d5bdf291173644bf99546c (diff)
Merge pull request #446 from dritter/fix_442
Protect special characters in various modes
-rw-r--r--.travis.yml2
-rw-r--r--functions/icons.zsh8
-rwxr-xr-xtest/functions/icons.spec362
3 files changed, 371 insertions, 1 deletions
diff --git a/.travis.yml b/.travis.yml
index ad4eb8c6..3e2439d3 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -21,9 +21,9 @@ script:
- test/powerlevel9k.spec
- test/functions/utilities.spec
- test/functions/colors.spec
+ - test/functions/icons.spec
- test/segments/command_execution_time.spec
- test/segments/dir.spec
- test/segments/rust_version.spec
- test/segments/go_version.spec
- test/segments/vcs.spec
-
diff --git a/functions/icons.zsh b/functions/icons.zsh
index 343b7685..413ea19b 100644
--- a/functions/icons.zsh
+++ b/functions/icons.zsh
@@ -86,6 +86,8 @@ case $POWERLEVEL9K_MODE in
'awesome-fontconfig')
# fontconfig with awesome-font required! See
# https://github.com/gabrielelana/awesome-terminal-fonts
+ # Set the right locale to protect special characters
+ local LC_ALL="" LC_CTYPE="en_US.UTF-8"
icons=(
LEFT_SEGMENT_SEPARATOR $'\uE0B0' # 
RIGHT_SEGMENT_SEPARATOR $'\uE0B2' # 
@@ -149,6 +151,8 @@ case $POWERLEVEL9K_MODE in
'nerdfont-fontconfig')
# nerd-font patched (complete) font required! See
# https://github.com/ryanoasis/nerd-fonts
+ # Set the right locale to protect special characters
+ local LC_ALL="" LC_CTYPE="en_US.UTF-8"
icons=(
LEFT_SEGMENT_SEPARATOR $'\uE0B0' # 
RIGHT_SEGMENT_SEPARATOR $'\uE0B2' # 
@@ -212,6 +216,8 @@ case $POWERLEVEL9K_MODE in
*)
# Powerline-Patched Font required!
# See https://github.com/Lokaltog/powerline-fonts
+ # Set the right locale to protect special characters
+ local LC_ALL="" LC_CTYPE="en_US.UTF-8"
icons=(
LEFT_SEGMENT_SEPARATOR $'\uE0B0' # 
RIGHT_SEGMENT_SEPARATOR $'\uE0B2' # 
@@ -277,6 +283,8 @@ esac
# Override the above icon settings with any user-defined variables.
case $POWERLEVEL9K_MODE in
'flat')
+ # Set the right locale to protect special characters
+ local LC_ALL="" LC_CTYPE="en_US.UTF-8"
icons[LEFT_SEGMENT_SEPARATOR]=''
icons[RIGHT_SEGMENT_SEPARATOR]=''
icons[LEFT_SUBSEGMENT_SEPARATOR]='|'
diff --git a/test/functions/icons.spec b/test/functions/icons.spec
new file mode 100755
index 00000000..c8d88644
--- /dev/null
+++ b/test/functions/icons.spec
@@ -0,0 +1,362 @@
+#!/usr/bin/env zsh
+#vim:ft=zsh ts=2 sw=2 sts=2 et fenc=utf-8
+
+# Required for shunit2 to run correctly
+setopt shwordsplit
+SHUNIT_PARENT=$0
+
+function setUp() {
+ # Store old value for LC_CTYPE
+ _OLD_LC_CTYPE="${LC_CTYPE}"
+ # Reset actual LC_CTYPE
+ unset LC_CTYPE
+
+ # Store old P9K mode
+ _OLD_P9K_MODE="${POWERLEVEL9K_MODE}"
+}
+
+function tearDown() {
+ # Restore LC_CTYPE
+ LC_CTYPE="${_OLD_LC_CTYPE}"
+
+ # Restore old P9K mode
+ POWERLEVEL9K_MODE="${_OLD_P9K_MODE}"
+}
+
+function testLcCtypeIsSetCorrectlyInDefaultMode() {
+ POWERLEVEL9K_MODE="default"
+ # Load Powerlevel9k
+ source functions/icons.zsh
+
+ assertEquals 'en_US.UTF-8' "${LC_CTYPE}"
+}
+
+function testLcCtypeIsSetCorrectlyInAwesomePatchedMode() {
+ POWERLEVEL9K_MODE="awesome-patched"
+ # Load Powerlevel9k
+ source functions/icons.zsh
+
+ assertEquals 'en_US.UTF-8' "${LC_CTYPE}"
+}
+
+function testLcCtypeIsSetCorrectlyInAwesomeFontconfigMode() {
+ POWERLEVEL9K_MODE="awesome-fontconfig"
+ # Load Powerlevel9k
+ source functions/icons.zsh
+
+ assertEquals 'en_US.UTF-8' "${LC_CTYPE}"
+}
+
+function testLcCtypeIsSetCorrectlyInNerdfontFontconfigMode() {
+ POWERLEVEL9K_MODE="nerdfont-fontconfig"
+ # Load Powerlevel9k
+ source functions/icons.zsh
+
+ assertEquals 'en_US.UTF-8' "${LC_CTYPE}"
+}
+
+function testLcCtypeIsSetCorrectlyInFlatMode() {
+ POWERLEVEL9K_MODE="flat"
+ # Load Powerlevel9k
+ source functions/icons.zsh
+
+ assertEquals 'en_US.UTF-8' "${LC_CTYPE}"
+}
+
+function testLcCtypeIsSetCorrectlyInCompatibleMode() {
+ POWERLEVEL9K_MODE="compatible"
+ # Load Powerlevel9k
+ source functions/icons.zsh
+
+ assertEquals 'en_US.UTF-8' "${LC_CTYPE}"
+}
+
+# Go through all icons defined in default mode, and
+# check if all of them are defined in the other modes.
+function testAllIconsAreDefinedLikeInDefaultMode() {
+ # Always compare against this mode
+ local _P9K_TEST_MODE="default"
+ POWERLEVEL9K_MODE="${_P9K_TEST_MODE}"
+ source functions/icons.zsh
+ # _ICONS_UNDER_TEST is an array of just the keys of $icons.
+ # We later check via (r) "subscript" flag that our key
+ # is in the values of our flat array.
+ typeset -ah _ICONS_UNDER_TEST
+ _ICONS_UNDER_TEST=(${(k)icons[@]})
+
+ # Switch to "awesome-patched" mode
+ POWERLEVEL9K_MODE="awesome-patched"
+ source functions/icons.zsh
+ typeset -ah current_icons
+ current_icons=(${(k)icons[@]})
+ for key in ${_ICONS_UNDER_TEST}; do
+ # Iterate over all keys found in the _ICONS_UNDER_TEST
+ # array and compare it with the icons array of the
+ # current POWERLEVEL9K_MODE.
+ # Use parameter expansion, to directly check if the
+ # key exists in the flat current array of keys. That
+ # is quite complicated, but there seems no easy way
+ # to check the mere existance of a key in an array.
+ # The usual way would always return the value, so that
+ # would do the wrong thing as we have some (on purpose)
+ # empty values.
+ assertTrue "The key ${key} does exist in ${_P9K_TEST_MODE} mode, but not in ${POWERLEVEL9K_MODE}!" "(( ${+current_icons[(r)$key]} ))"
+ done
+
+ # Switch to "awesome-fontconfig" mode
+ POWERLEVEL9K_MODE="awesome-fontconfig"
+ source functions/icons.zsh
+ typeset -ah current_icons
+ current_icons=(${(k)icons[@]})
+ for key in ${_ICONS_UNDER_TEST}; do
+ assertTrue "The key ${key} does exist in ${_P9K_TEST_MODE} mode, but not in ${POWERLEVEL9K_MODE}!" "(( ${+current_icons[(r)$key]} ))"
+ done
+
+ # Switch to "nerdfont-fontconfig" mode
+ POWERLEVEL9K_MODE="nerdfont-fontconfig"
+ source functions/icons.zsh
+ typeset -ah current_icons
+ current_icons=(${(k)icons[@]})
+ for key in ${_ICONS_UNDER_TEST}; do
+ assertTrue "The key ${key} does exist in ${_P9K_TEST_MODE} mode, but not in ${POWERLEVEL9K_MODE}!" "(( ${+current_icons[(r)$key]} ))"
+ done
+
+ # Switch to "flat" mode
+ POWERLEVEL9K_MODE="flat"
+ source functions/icons.zsh
+ typeset -ah current_icons
+ current_icons=(${(k)icons[@]})
+ for key in ${_ICONS_UNDER_TEST}; do
+ assertTrue "The key ${key} does exist in ${_P9K_TEST_MODE} mode, but not in ${POWERLEVEL9K_MODE}!" "(( ${+current_icons[(r)$key]} ))"
+ done
+
+ # Switch to "compatible" mode
+ POWERLEVEL9K_MODE="compatible"
+ source functions/icons.zsh
+ typeset -ah current_icons
+ current_icons=(${(k)icons[@]})
+ for key in ${_ICONS_UNDER_TEST}; do
+ assertTrue "The key ${key} does exist in ${_P9K_TEST_MODE} mode, but not in ${POWERLEVEL9K_MODE}!" "(( ${+current_icons[(r)$key]} ))"
+ done
+
+ unset current_icons
+ unset _ICONS_UNDER_TEST
+}
+
+# Go through all icons defined in awesome-patched mode, and
+# check if all of them are defined in the other modes.
+function testAllIconsAreDefinedLikeInAwesomePatchedMode() {
+ # Always compare against this mode
+ local _P9K_TEST_MODE="awesome-patched"
+ POWERLEVEL9K_MODE="$_P9K_TEST_MODE"
+ source functions/icons.zsh
+ # _ICONS_UNDER_TEST is an array of just the keys of $icons.
+ # We later check via (r) "subscript" flag that our key
+ # is in the values of our flat array.
+ typeset -ah _ICONS_UNDER_TEST
+ _ICONS_UNDER_TEST=(${(k)icons[@]})
+
+ # Switch to "default" mode
+ POWERLEVEL9K_MODE="default"
+ source functions/icons.zsh
+ typeset -ah current_icons
+ current_icons=(${(k)icons[@]})
+ for key in ${_ICONS_UNDER_TEST}; do
+ # Iterate over all keys found in the _ICONS_UNDER_TEST
+ # array and compare it with the icons array of the
+ # current POWERLEVEL9K_MODE.
+ # Use parameter expansion, to directly check if the
+ # key exists in the flat current array of keys. That
+ # is quite complicated, but there seems no easy way
+ # to check the mere existance of a key in an array.
+ # The usual way would always return the value, so that
+ # would do the wrong thing as we have some (on purpose)
+ # empty values.
+ assertTrue "The key ${key} does exist in ${_P9K_TEST_MODE} mode, but not in ${POWERLEVEL9K_MODE}!" "(( ${+current_icons[(r)$key]} ))"
+ done
+
+ # Switch to "awesome-fontconfig" mode
+ POWERLEVEL9K_MODE="awesome-fontconfig"
+ source functions/icons.zsh
+ typeset -ah current_icons
+ current_icons=(${(k)icons[@]})
+ for key in ${_ICONS_UNDER_TEST}; do
+ assertTrue "The key ${key} does exist in ${_P9K_TEST_MODE} mode, but not in ${POWERLEVEL9K_MODE}!" "(( ${+current_icons[(r)$key]} ))"
+ done
+
+ # Switch to "nerdfont-fontconfig" mode
+ POWERLEVEL9K_MODE="nerdfont-fontconfig"
+ source functions/icons.zsh
+ typeset -ah current_icons
+ current_icons=(${(k)icons[@]})
+ for key in ${_ICONS_UNDER_TEST}; do
+ assertTrue "The key ${key} does exist in ${_P9K_TEST_MODE} mode, but not in ${POWERLEVEL9K_MODE}!" "(( ${+current_icons[(r)$key]} ))"
+ done
+
+ # Switch to "flat" mode
+ POWERLEVEL9K_MODE="flat"
+ source functions/icons.zsh
+ typeset -ah current_icons
+ current_icons=(${(k)icons[@]})
+ for key in ${_ICONS_UNDER_TEST}; do
+ assertTrue "The key ${key} does exist in ${_P9K_TEST_MODE} mode, but not in ${POWERLEVEL9K_MODE}!" "(( ${+current_icons[(r)$key]} ))"
+ done
+
+ # Switch to "compatible" mode
+ POWERLEVEL9K_MODE="compatible"
+ source functions/icons.zsh
+ typeset -ah current_icons
+ current_icons=(${(k)icons[@]})
+ for key in ${_ICONS_UNDER_TEST}; do
+ assertTrue "The key ${key} does exist in ${_P9K_TEST_MODE} mode, but not in ${POWERLEVEL9K_MODE}!" "(( ${+current_icons[(r)$key]} ))"
+ done
+
+ unset current_icons
+ unset _ICONS_UNDER_TEST
+}
+
+# Go through all icons defined in awesome-fontconfig mode, and
+# check if all of them are defined in the other modes.
+function testAllIconsAreDefinedLikeInAwesomeFontconfigMode() {
+ # Always compare against this mode
+ local _P9K_TEST_MODE="awesome-fontconfig"
+ POWERLEVEL9K_MODE="$_P9K_TEST_MODE"
+ source functions/icons.zsh
+ # _ICONS_UNDER_TEST is an array of just the keys of $icons.
+ # We later check via (r) "subscript" flag that our key
+ # is in the values of our flat array.
+ typeset -ah _ICONS_UNDER_TEST
+ _ICONS_UNDER_TEST=(${(k)icons[@]})
+
+ # Switch to "default" mode
+ POWERLEVEL9K_MODE="default"
+ source functions/icons.zsh
+ typeset -ah current_icons
+ current_icons=(${(k)icons[@]})
+ for key in ${_ICONS_UNDER_TEST}; do
+ # Iterate over all keys found in the _ICONS_UNDER_TEST
+ # array and compare it with the icons array of the
+ # current POWERLEVEL9K_MODE.
+ # Use parameter expansion, to directly check if the
+ # key exists in the flat current array of keys. That
+ # is quite complicated, but there seems no easy way
+ # to check the mere existance of a key in an array.
+ # The usual way would always return the value, so that
+ # would do the wrong thing as we have some (on purpose)
+ # empty values.
+ assertTrue "The key ${key} does exist in ${_P9K_TEST_MODE} mode, but not in ${POWERLEVEL9K_MODE}!" "(( ${+current_icons[(r)$key]} ))"
+ done
+
+ # Switch to "awesome-patched" mode
+ POWERLEVEL9K_MODE="awesome-patched"
+ source functions/icons.zsh
+ typeset -ah current_icons
+ current_icons=(${(k)icons[@]})
+ for key in ${_ICONS_UNDER_TEST}; do
+ assertTrue "The key ${key} does exist in ${_P9K_TEST_MODE} mode, but not in ${POWERLEVEL9K_MODE}!" "(( ${+current_icons[(r)$key]} ))"
+ done
+
+ # Switch to "nerdfont-fontconfig" mode
+ POWERLEVEL9K_MODE="nerdfont-fontconfig"
+ source functions/icons.zsh
+ typeset -ah current_icons
+ current_icons=(${(k)icons[@]})
+ for key in ${_ICONS_UNDER_TEST}; do
+ assertTrue "The key ${key} does exist in ${_P9K_TEST_MODE} mode, but not in ${POWERLEVEL9K_MODE}!" "(( ${+current_icons[(r)$key]} ))"
+ done
+
+ # Switch to "flat" mode
+ POWERLEVEL9K_MODE="flat"
+ source functions/icons.zsh
+ typeset -ah current_icons
+ current_icons=(${(k)icons[@]})
+ for key in ${_ICONS_UNDER_TEST}; do
+ assertTrue "The key ${key} does exist in ${_P9K_TEST_MODE} mode, but not in ${POWERLEVEL9K_MODE}!" "(( ${+current_icons[(r)$key]} ))"
+ done
+
+ # Switch to "compatible" mode
+ POWERLEVEL9K_MODE="compatible"
+ source functions/icons.zsh
+ typeset -ah current_icons
+ current_icons=(${(k)icons[@]})
+ for key in ${_ICONS_UNDER_TEST}; do
+ assertTrue "The key ${key} does exist in ${_P9K_TEST_MODE} mode, but not in ${POWERLEVEL9K_MODE}!" "(( ${+current_icons[(r)$key]} ))"
+ done
+
+ unset current_icons
+ unset _ICONS_UNDER_TEST
+}
+
+# Go through all icons defined in nerdfont-fontconfig mode, and
+# check if all of them are defined in the other modes.
+function testAllIconsAreDefinedLikeInNerdfontFontconfigMode() {
+ # Always compare against this mode
+ local _P9K_TEST_MODE="nerdfont-fontconfig"
+ POWERLEVEL9K_MODE="$_P9K_TEST_MODE"
+ source functions/icons.zsh
+ # _ICONS_UNDER_TEST is an array of just the keys of $icons.
+ # We later check via (r) "subscript" flag that our key
+ # is in the values of our flat array.
+ typeset -ah _ICONS_UNDER_TEST
+ _ICONS_UNDER_TEST=(${(k)icons[@]})
+
+ # Switch to "default" mode
+ POWERLEVEL9K_MODE="default"
+ source functions/icons.zsh
+ typeset -ah current_icons
+ current_icons=(${(k)icons[@]})
+ for key in ${_ICONS_UNDER_TEST}; do
+ # Iterate over all keys found in the _ICONS_UNDER_TEST
+ # array and compare it with the icons array of the
+ # current POWERLEVEL9K_MODE.
+ # Use parameter expansion, to directly check if the
+ # key exists in the flat current array of keys. That
+ # is quite complicated, but there seems no easy way
+ # to check the mere existance of a key in an array.
+ # The usual way would always return the value, so that
+ # would do the wrong thing as we have some (on purpose)
+ # empty values.
+ assertTrue "The key ${key} does exist in ${_P9K_TEST_MODE} mode, but not in ${POWERLEVEL9K_MODE}!" "(( ${+current_icons[(r)$key]} ))"
+ done
+
+ # Switch to "awesome-patched" mode
+ POWERLEVEL9K_MODE="awesome-patched"
+ source functions/icons.zsh
+ typeset -ah current_icons
+ current_icons=(${(k)icons[@]})
+ for key in ${_ICONS_UNDER_TEST}; do
+ assertTrue "The key ${key} does exist in ${_P9K_TEST_MODE} mode, but not in ${POWERLEVEL9K_MODE}!" "(( ${+current_icons[(r)$key]} ))"
+ done
+
+ # Switch to "awesome-fontconfig" mode
+ POWERLEVEL9K_MODE="awesome-fontconfig"
+ source functions/icons.zsh
+ typeset -ah current_icons
+ current_icons=(${(k)icons[@]})
+ for key in ${_ICONS_UNDER_TEST}; do
+ assertTrue "The key ${key} does exist in ${_P9K_TEST_MODE} mode, but not in ${POWERLEVEL9K_MODE}!" "(( ${+current_icons[(r)$key]} ))"
+ done
+
+ # Switch to "flat" mode
+ POWERLEVEL9K_MODE="flat"
+ source functions/icons.zsh
+ typeset -ah current_icons
+ current_icons=(${(k)icons[@]})
+ for key in ${_ICONS_UNDER_TEST}; do
+ assertTrue "The key ${key} does exist in ${_P9K_TEST_MODE} mode, but not in ${POWERLEVEL9K_MODE}!" "(( ${+current_icons[(r)$key]} ))"
+ done
+
+ # Switch to "compatible" mode
+ POWERLEVEL9K_MODE="compatible"
+ source functions/icons.zsh
+ typeset -ah current_icons
+ current_icons=(${(k)icons[@]})
+ for key in ${_ICONS_UNDER_TEST}; do
+ assertTrue "The key ${key} does exist in ${_P9K_TEST_MODE} mode, but not in ${POWERLEVEL9K_MODE}!" "(( ${+current_icons[(r)$key]} ))"
+ done
+
+ unset current_icons
+ unset _ICONS_UNDER_TEST
+}
+
+source shunit2/source/2.1/src/shunit2 \ No newline at end of file