From 42c39c8922b0fb03c0bcc6c2101165412ed50e4c Mon Sep 17 00:00:00 2001 From: Dominik Ritter Date: Tue, 9 Feb 2016 00:45:33 +0100 Subject: [PATCH 01/17] Add Unit-Tests Add unittests via shunit2 (https://github.com/kward/shunit2), that integrate with travis-ci.org. --- .gitmodules | 3 +++ .travis.yml | 9 ++++++++ shunit2 | 1 + test/powerlevel9k.spec | 48 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 61 insertions(+) create mode 100644 .gitmodules create mode 100644 .travis.yml create mode 160000 shunit2 create mode 100755 test/powerlevel9k.spec diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 00000000..b887685f --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "shunit2"] + path = shunit2 + url = git@github.com:kward/shunit2.git diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 00000000..818940cc --- /dev/null +++ b/.travis.yml @@ -0,0 +1,9 @@ +language: php +sudo: false + +php: + - '7.0' + +script: + - test/powerlevel9k.spec + diff --git a/shunit2 b/shunit2 new file mode 160000 index 00000000..60dd60bc --- /dev/null +++ b/shunit2 @@ -0,0 +1 @@ +Subproject commit 60dd60bcd1573befe38465010263ab242e55811d diff --git a/test/powerlevel9k.spec b/test/powerlevel9k.spec new file mode 100755 index 00000000..3ed33d64 --- /dev/null +++ b/test/powerlevel9k.spec @@ -0,0 +1,48 @@ +#!/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 oneTimeSetUp() { + # Load Powerlevel9k + source powerlevel9k.zsh-theme + source functions/* +} + +function testJoinedSegments() { + POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(dir dir_joined) + + assertEquals "%K{blue} %F{black}%~ %K{blue}%F{black}%F{black}%~ %k%F{blue}%f " "$(build_left_prompt)" + + unset POWERLEVEL9K_LEFT_PROMPT_ELEMENTS +} + +function testTransitiveJoinedSegments() { + POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(dir root_indicator_joined dir_joined) + + assertEquals "%K{blue} %F{black}%~ %K{blue}%F{black}%F{black}%~ %k%F{blue}%f " "$(build_left_prompt)" + + unset POWERLEVEL9K_LEFT_PROMPT_ELEMENTS +} + +function testJoiningWithConditionalSegment() { + POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(dir background_jobs dir_joined) + + assertEquals "%K{blue} %F{black}%~ %K{blue}%F{black} %F{black}%~ %k%F{blue}%f " "$(build_left_prompt)" + + unset POWERLEVEL9K_LEFT_PROMPT_ELEMENTS +} + +function testDynamicColoringOfSegmentsWork() { + POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(dir) + POWERLEVEL9K_DIR_HOME_SUBFOLDER_BACKGROUND='red' + + assertEquals "%K{red} %F{black}%~ %k%F{red}%f " "$(build_left_prompt)" + + unset POWERLEVEL9K_LEFT_PROMPT_ELEMENTS + unset POWERLEVEL9K_DIR_HOME_SUBFOLDER_BACKGROUND +} + +source shunit2/source/2.1/src/shunit2 From ded4653bf268c06757352be0fb1dfafb462ae506 Mon Sep 17 00:00:00 2001 From: Dominik Ritter Date: Tue, 9 Feb 2016 00:53:40 +0100 Subject: [PATCH 02/17] Install ZSH on Travis-CI --- .travis.yml | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/.travis.yml b/.travis.yml index 818940cc..e2191a7c 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,9 +1,22 @@ -language: php -sudo: false +sudo: true +language: sh +addons: + apt: + packages: + - build-essential -php: - - '7.0' +before_script: + # Show the git version being used to test. + - "git --version" + # Show the zsh version being used to test. + - "zsh --version" + +install: + - "sudo apt-add-repository -y ppa:brainpower/testing" + - "sudo apt-get update -qq" + - "sudo apt-get install zsh" + - "sudo chsh -s $(which zsh)" script: - - test/powerlevel9k.spec + - test/powerlevel9k.spec From be0dce7ff6606bc0379ed209649a9fe74091d786 Mon Sep 17 00:00:00 2001 From: Dominik Ritter Date: Tue, 9 Feb 2016 09:49:19 +0100 Subject: [PATCH 03/17] Load submodules from HTTPS --- .gitmodules | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitmodules b/.gitmodules index b887685f..743b8e91 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +1,3 @@ [submodule "shunit2"] path = shunit2 - url = git@github.com:kward/shunit2.git + url = https://github.com/kward/shunit2.git From ca6cbaa0b663b580c62d3187616af4d0aed79758 Mon Sep 17 00:00:00 2001 From: Dominik Ritter Date: Tue, 9 Feb 2016 21:43:57 +0100 Subject: [PATCH 04/17] Add unit tests for printSizeHumanReadable --- .travis.yml | 1 + test/functions/utilities.spec | 22 ++++++++++++++++++++++ 2 files changed, 23 insertions(+) create mode 100755 test/functions/utilities.spec diff --git a/.travis.yml b/.travis.yml index e2191a7c..33f99bbd 100644 --- a/.travis.yml +++ b/.travis.yml @@ -19,4 +19,5 @@ install: script: - test/powerlevel9k.spec + - test/functions/utilities.spec diff --git a/test/functions/utilities.spec b/test/functions/utilities.spec new file mode 100755 index 00000000..12501348 --- /dev/null +++ b/test/functions/utilities.spec @@ -0,0 +1,22 @@ +#!/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 oneTimeSetUp() { + # Load Powerlevel9k + source functions/utilities.zsh +} + +function testPrintSizeHumanReadableWithBigNumber() { + # Interesting: Currently we can't support numbers bigger than that. + assertEquals '0.87E' "$(printSizeHumanReadable 1000000000000000000)" +} + +function testPrintSizeHumanReadableWithExabytesAsBase() { + assertEquals '9.77Z' "$(printSizeHumanReadable 10000 'E')" +} + +source shunit2/source/2.1/src/shunit2 From d7a0f493912ecdfe117c7e67fac0a355f34bb4a4 Mon Sep 17 00:00:00 2001 From: Dominik Ritter Date: Tue, 9 Feb 2016 21:44:45 +0100 Subject: [PATCH 05/17] Add documentation for printSizeHumanReadable --- functions/utilities.zsh | 3 +++ 1 file changed, 3 insertions(+) diff --git a/functions/utilities.zsh b/functions/utilities.zsh index 079e675a..7ca73b36 100644 --- a/functions/utilities.zsh +++ b/functions/utilities.zsh @@ -27,6 +27,9 @@ function set_default() { } # Converts large memory values into a human-readable unit (e.g., bytes --> GB) +# Takes two arguments: +# * $size - The number which should be prettified +# * $base - The base of the number (default Bytes) printSizeHumanReadable() { typeset -F 2 size size="$1"+0.00001 From c0c611c21d9913bf333bc54b71af67b70107d0f9 Mon Sep 17 00:00:00 2001 From: Dominik Ritter Date: Fri, 12 Feb 2016 00:30:48 +0100 Subject: [PATCH 06/17] Add tests for functions/utilities.zsh In particular: `defined`, `set_default`, `getRelevantItem` and `segmentShouldBeJoined` --- test/functions/utilities.spec | 86 +++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) diff --git a/test/functions/utilities.spec b/test/functions/utilities.spec index 12501348..ad0f4efb 100755 --- a/test/functions/utilities.spec +++ b/test/functions/utilities.spec @@ -10,6 +10,24 @@ function oneTimeSetUp() { source functions/utilities.zsh } +function testDefinedFindsDefinedVariable() { + my_var='X' + + assertTrue "defined 'my_var'" + unset my_var +} + +function testDefinedDoesNotFindUndefinedVariable() { + assertFalse "defined 'my_var'" +} + +function testSetDefaultSetsVariable() { + set_default 'my_var' 'x' + + assertEquals 'x' "$my_var" + unset my_var +} + function testPrintSizeHumanReadableWithBigNumber() { # Interesting: Currently we can't support numbers bigger than that. assertEquals '0.87E' "$(printSizeHumanReadable 1000000000000000000)" @@ -19,4 +37,72 @@ function testPrintSizeHumanReadableWithExabytesAsBase() { assertEquals '9.77Z' "$(printSizeHumanReadable 10000 'E')" } +function testGetRelevantItem() { + typeset -a list + list=(a b c) + local callback='[[ "$item" == "b" ]] && echo "found"' + + local result=$(getRelevantItem "$list" "$callback") + assertEquals 'found' "$result" + + unset list +} + +function testGetRelevantItemDoesNotReturnNotFoundItems() { + typeset -a list + list=(a b c) + local callback='[[ "$item" == "d" ]] && echo "found"' + + local result=$(getRelevantItem "$list" "$callback") + assertEquals '' '' + + unset list +} + +function testSegmentShouldBeJoinedIfDirectPredecessingSegmentIsJoined() { + typeset -a segments + segments=(a b_joined c_joined) + # Look at the third segment + local current_index=3 + local last_element_index=2 + + local joined + segmentShouldBeJoined $current_index $last_element_index "$segments" && joined=true || joined=false + assertTrue "$joined" + + unset segments +} + +function testSegmentShouldBeJoinedIfPredecessingSegmentIsJoinedTransitivley() { + typeset -a segments + segments=(a b_joined c_joined) + # Look at the third segment + local current_index=3 + # The last printed segment was the first one, + # the second segmend was conditional. + local last_element_index=1 + + local joined + segmentShouldBeJoined $current_index $last_element_index "$segments" && joined=true || joined=false + assertTrue "$joined" + + unset segments +} + +function testSegmentShouldNotBeJoinedIfPredecessingSegmentIsNotJoinedButConditional() { + typeset -a segments + segments=(a b_joined c d_joined) + # Look at the fourth segment + local current_index=4 + # The last printed segment was the first one, + # the second segmend was conditional. + local last_element_index=1 + + local joined + segmentShouldBeJoined $current_index $last_element_index "$segments" && joined=true || joined=false + assertFalse "$joined" + + unset segments +} + source shunit2/source/2.1/src/shunit2 From 8a386ba1b39e574b00e002dbb4e9e0f0c8c3f59e Mon Sep 17 00:00:00 2001 From: Dominik Ritter Date: Fri, 12 Feb 2016 01:12:18 +0100 Subject: [PATCH 07/17] Add tests for functions/colors.zsh --- .travis.yml | 1 + test/functions/colors.spec | 42 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+) create mode 100755 test/functions/colors.spec diff --git a/.travis.yml b/.travis.yml index 33f99bbd..7e4f8d11 100644 --- a/.travis.yml +++ b/.travis.yml @@ -20,4 +20,5 @@ install: script: - test/powerlevel9k.spec - test/functions/utilities.spec + - test/functions/colors.spec diff --git a/test/functions/colors.spec b/test/functions/colors.spec new file mode 100755 index 00000000..d1c5a6fc --- /dev/null +++ b/test/functions/colors.spec @@ -0,0 +1,42 @@ +#!/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 oneTimeSetUp() { + # Load Powerlevel9k + source functions/colors.zsh +} + +function testGetColorCodeWithAnsiForegroundColor() { + assertEquals '002' "$(getColorCode 'green')" +} + +function testGetColorCodeWithAnsiBackgroundColor() { + assertEquals '002' "$(getColorCode 'bg-green')" +} + +function testGetColorCodeWithNumericalColor() { + assertEquals '002' "$(getColorCode '002')" +} + +function testIsSameColorComparesAnsiForegroundAndNumericalColorCorrectly() { + assertTrue "isSameColor 'green' '002'" +} + +function testIsSameColorComparesAnsiBackgroundAndNumericalColorCorrectly() { + assertTrue "isSameColor 'bg-green' '002'" +} + +function testIsSameColorComparesNumericalBackgroundAndNumericalColorCorrectly() { + assertTrue "isSameColor '010' '2'" +} + +function testIsSameColorDoesNotYieldNotEqualColorsTruthy() { + assertFalse "isSameColor 'green' '003'" +} + + +source shunit2/source/2.1/src/shunit2 From a75b0c17d25cbada32458a71a2d8ada07a35f026 Mon Sep 17 00:00:00 2001 From: Dominik Ritter Date: Sat, 13 Feb 2016 17:25:03 +0100 Subject: [PATCH 08/17] Add tests for visual identifiers --- test/powerlevel9k.spec | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/test/powerlevel9k.spec b/test/powerlevel9k.spec index 3ed33d64..7a85e516 100755 --- a/test/powerlevel9k.spec +++ b/test/powerlevel9k.spec @@ -45,4 +45,40 @@ function testDynamicColoringOfSegmentsWork() { unset POWERLEVEL9K_DIR_HOME_SUBFOLDER_BACKGROUND } +function testDynamicColoringOfVisualIdentifiersWork() { + POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(dir) + POWERLEVEL9K_MODE='awesome-patched' + POWERLEVEL9K_DIR_HOME_SUBFOLDER_VISUAL_IDENTIFIER_COLOR='green' + + # Re-Source the icons, as the POWERLEVEL9K_MODE is directly + # evaluated there. + source functions/icons.zsh + + assertEquals "%K{blue} %F{green%}%f %F{black}%~ %k%F{blue}%f " "$(build_left_prompt)" + + unset POWERLEVEL9K_LEFT_PROMPT_ELEMENTS + unset POWERLEVEL9K_MODE + unset POWERLEVEL9K_DIR_HOME_SUBFOLDER_VISUAL_IDENTIFIER_COLOR +} + +function testColoringOfVisualIdentifiersDoesNotOverwriteColoringOfSegment() { + POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(dir) + POWERLEVEL9K_MODE='awesome-patched' + POWERLEVEL9K_DIR_HOME_SUBFOLDER_VISUAL_IDENTIFIER_COLOR='green' + POWERLEVEL9K_DIR_HOME_SUBFOLDER_FOREGROUND='red' + POWERLEVEL9K_DIR_HOME_SUBFOLDER_BACKGROUND='yellow' + + # Re-Source the icons, as the POWERLEVEL9K_MODE is directly + # evaluated there. + source functions/icons.zsh + + assertEquals "%K{yellow} %F{green%}%f %F{red}%~ %k%F{yellow}%f " "$(build_left_prompt)" + + unset POWERLEVEL9K_LEFT_PROMPT_ELEMENTS + unset POWERLEVEL9K_MODE + unset POWERLEVEL9K_DIR_HOME_SUBFOLDER_VISUAL_IDENTIFIER_COLOR + unset POWERLEVEL9K_DIR_HOME_SUBFOLDER_FOREGROUND + unset POWERLEVEL9K_DIR_HOME_SUBFOLDER_BACKGROUND +} + source shunit2/source/2.1/src/shunit2 From a3eaf4a4b0098918d3b39dbe705fa587032c5648 Mon Sep 17 00:00:00 2001 From: Dominik Ritter Date: Sat, 13 Feb 2016 17:35:06 +0100 Subject: [PATCH 09/17] Improve isolation of tests --- test/functions/colors.spec | 2 +- test/functions/utilities.spec | 3 ++- test/powerlevel9k.spec | 2 +- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/test/functions/colors.spec b/test/functions/colors.spec index d1c5a6fc..61a40087 100755 --- a/test/functions/colors.spec +++ b/test/functions/colors.spec @@ -5,7 +5,7 @@ setopt shwordsplit SHUNIT_PARENT=$0 -function oneTimeSetUp() { +function setUp() { # Load Powerlevel9k source functions/colors.zsh } diff --git a/test/functions/utilities.spec b/test/functions/utilities.spec index ad0f4efb..a9bd67e3 100755 --- a/test/functions/utilities.spec +++ b/test/functions/utilities.spec @@ -5,8 +5,9 @@ setopt shwordsplit SHUNIT_PARENT=$0 -function oneTimeSetUp() { +function setUp() { # Load Powerlevel9k + source functions/icons.zsh source functions/utilities.zsh } diff --git a/test/powerlevel9k.spec b/test/powerlevel9k.spec index 7a85e516..06f0868e 100755 --- a/test/powerlevel9k.spec +++ b/test/powerlevel9k.spec @@ -5,7 +5,7 @@ setopt shwordsplit SHUNIT_PARENT=$0 -function oneTimeSetUp() { +function setUp() { # Load Powerlevel9k source powerlevel9k.zsh-theme source functions/* From 9c0b8b9e42d3b65be3d3c5914d53788219737b5f Mon Sep 17 00:00:00 2001 From: Dominik Ritter Date: Sat, 13 Feb 2016 17:37:55 +0100 Subject: [PATCH 10/17] Add test for overwriting icons --- test/powerlevel9k.spec | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/test/powerlevel9k.spec b/test/powerlevel9k.spec index 06f0868e..1a6d3bff 100755 --- a/test/powerlevel9k.spec +++ b/test/powerlevel9k.spec @@ -81,4 +81,14 @@ function testColoringOfVisualIdentifiersDoesNotOverwriteColoringOfSegment() { unset POWERLEVEL9K_DIR_HOME_SUBFOLDER_BACKGROUND } +function testOverwritingIconsWork() { + POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(dir) + POWERLEVEL9K_HOME_SUB_ICON='icon-here' + + assertEquals "%K{blue} %F{black%}icon-here%f %F{black}%~ %k%F{blue}%f " "$(build_left_prompt)" + + unset POWERLEVEL9K_LEFT_PROMPT_ELEMENTS + unset POWERLEVEL9K_DIR_HOME_SUB_ICON +} + source shunit2/source/2.1/src/shunit2 From 4844bbbaea2efad343a2478fda8777752dc1ae85 Mon Sep 17 00:00:00 2001 From: Dominik Ritter Date: Sat, 13 Feb 2016 17:41:26 +0100 Subject: [PATCH 11/17] Avoid color warning on travis --- test/powerlevel9k.spec | 1 + 1 file changed, 1 insertion(+) diff --git a/test/powerlevel9k.spec b/test/powerlevel9k.spec index 1a6d3bff..1aab2c5f 100755 --- a/test/powerlevel9k.spec +++ b/test/powerlevel9k.spec @@ -6,6 +6,7 @@ setopt shwordsplit SHUNIT_PARENT=$0 function setUp() { + export TERM="xterm-256color" # Load Powerlevel9k source powerlevel9k.zsh-theme source functions/* From 7cf46711cca890244b4331629c060c4d19840de3 Mon Sep 17 00:00:00 2001 From: Dominik Ritter Date: Sat, 13 Feb 2016 19:23:30 +0100 Subject: [PATCH 12/17] Add tests for the dir segment --- .travis.yml | 1 + test/segments/dir.spec | 118 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 119 insertions(+) create mode 100755 test/segments/dir.spec diff --git a/.travis.yml b/.travis.yml index 7e4f8d11..a2e2a0e5 100644 --- a/.travis.yml +++ b/.travis.yml @@ -21,4 +21,5 @@ script: - test/powerlevel9k.spec - test/functions/utilities.spec - test/functions/colors.spec + - test/segments/dir.spec diff --git a/test/segments/dir.spec b/test/segments/dir.spec new file mode 100755 index 00000000..035addea --- /dev/null +++ b/test/segments/dir.spec @@ -0,0 +1,118 @@ +#!/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() { + export TERM="xterm-256color" + # Load Powerlevel9k + source powerlevel9k.zsh-theme +} + +function testTruncateFoldersWorks() { + POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(dir) + POWERLEVEL9K_SHORTEN_DIR_LENGTH=2 + POWERLEVEL9K_SHORTEN_STRATEGY='truncate_folders' + + FOLDER=/tmp/powerlevel9k-test/1/12/123/1234/12345/123456/1234567/12345678/123456789 + mkdir -p $FOLDER + cd $FOLDER + + assertEquals "%K{blue} %F{black}%3(c:…/:)%2c %k%F{blue}%f " "$(build_left_prompt)" + + cd - + rm -fr /tmp/powerlevel9k-test + + unset FOLDER + unset POWERLEVEL9K_LEFT_PROMPT_ELEMENTS + unset POWERLEVEL9K_SHORTEN_DIR_LENGTH + unset POWERLEVEL9K_SHORTEN_STRATEGY +} + +function testTruncateMiddleWorks() { + POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(dir) + POWERLEVEL9K_SHORTEN_DIR_LENGTH=2 + POWERLEVEL9K_SHORTEN_STRATEGY='truncate_middle' + + FOLDER=/tmp/powerlevel9k-test/1/12/123/1234/12345/123456/1234567/12345678/123456789 + mkdir -p $FOLDER + cd $FOLDER + + assertEquals "%K{blue} %F{black}/tmp/po…st/1/12/123/1234/12…45/12…56/12…67/12…78/123456789 %k%F{blue}%f " "$(build_left_prompt)" + + cd - + rm -fr /tmp/powerlevel9k-test + + unset FOLDER + unset POWERLEVEL9K_LEFT_PROMPT_ELEMENTS + unset POWERLEVEL9K_SHORTEN_DIR_LENGTH + unset POWERLEVEL9K_SHORTEN_STRATEGY +} + +function testTruncationFromRightWorks() { + POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(dir) + POWERLEVEL9K_SHORTEN_DIR_LENGTH=2 + POWERLEVEL9K_SHORTEN_STRATEGY='truncate_from_right' + + FOLDER=/tmp/powerlevel9k-test/1/12/123/1234/12345/123456/1234567/12345678/123456789 + mkdir -p $FOLDER + cd $FOLDER + + assertEquals "%K{blue} %F{black}/tm…/po…/1/12/12…/12…/12…/12…/12…/12…/123456789 %k%F{blue}%f " "$(build_left_prompt)" + + cd - + rm -fr /tmp/powerlevel9k-test + + unset FOLDER + unset POWERLEVEL9K_LEFT_PROMPT_ELEMENTS + unset POWERLEVEL9K_SHORTEN_DIR_LENGTH + unset POWERLEVEL9K_SHORTEN_STRATEGY +} + +function testHomeFolderDetectionWorks() { + POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(dir) + POWERLEVEL9K_HOME_ICON='home-icon' + + cd ~ + assertEquals "%K{blue} %F{black%}home-icon%f %F{black}%~ %k%F{blue}%f " "$(build_left_prompt)" + + cd - + unset POWERLEVEL9K_LEFT_PROMPT_ELEMENTS + unset POWERLEVEL9K_HOME_ICON +} + +function testHomeSubfolderDetectionWorks() { + POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(dir) + POWERLEVEL9K_HOME_SUB_ICON='sub-icon' + + FOLDER=~/powerlevel9k-test + mkdir $FOLDER + cd $FOLDER + assertEquals "%K{blue} %F{black%}sub-icon%f %F{black}%~ %k%F{blue}%f " "$(build_left_prompt)" + + cd - + rm -fr $FOLDER + unset FOLDER + unset POWERLEVEL9K_LEFT_PROMPT_ELEMENTS + unset POWERLEVEL9K_HOME_SUB_ICON +} + +function testOtherFolderDetectionWorks() { + POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(dir) + POWERLEVEL9K_FOLDER_ICON='folder-icon' + + FOLDER=/tmp/powerlevel9k-test + mkdir $FOLDER + cd $FOLDER + assertEquals "%K{blue} %F{black%}folder-icon%f %F{black}%~ %k%F{blue}%f " "$(build_left_prompt)" + + cd - + rm -fr $FOLDER + unset FOLDER + unset POWERLEVEL9K_LEFT_PROMPT_ELEMENTS + unset POWERLEVEL9K_FOLDER_ICON +} + +source shunit2/source/2.1/src/shunit2 From 0d8c05ecb7b7a02a42636b1d3600af5535e43d81 Mon Sep 17 00:00:00 2001 From: Dominik Ritter Date: Sat, 13 Feb 2016 19:54:31 +0100 Subject: [PATCH 13/17] Run tests on trusty, where ZSH 5.0.2 is available --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index a2e2a0e5..e9afcc29 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,4 +1,5 @@ sudo: true +dist: trusty language: sh addons: apt: @@ -12,7 +13,6 @@ before_script: - "zsh --version" install: - - "sudo apt-add-repository -y ppa:brainpower/testing" - "sudo apt-get update -qq" - "sudo apt-get install zsh" - "sudo chsh -s $(which zsh)" From 34bf1f0bf571d5116dc783f0d50d0aa99ff5c5c1 Mon Sep 17 00:00:00 2001 From: Dominik Ritter Date: Sat, 13 Feb 2016 21:49:34 +0100 Subject: [PATCH 14/17] Add documentation for Unit Tests --- TESTS.md | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 TESTS.md diff --git a/TESTS.md b/TESTS.md new file mode 100644 index 00000000..d4e663dc --- /dev/null +++ b/TESTS.md @@ -0,0 +1,14 @@ +# Structure + +The Unit-Tests do not follow exactly the file structure of Powerlevel9k itself. + +## Basic Tests + +Basic Tests belong in `test/powerlevel9k.spec` if they test basic functionality of +Powerlevel9k itself. Basic functions from the `functions` directory have their +Tests in separate files under `test/functions`. + +## Segment Tests + +These Tests tend to be more complex in setup than the basic tests. To avoid ending +up in a huge single file, there is one file per segment in `test/segments`. From 490bafc8682b49e1ba321f51fa3c99e49529e702 Mon Sep 17 00:00:00 2001 From: Dominik Ritter Date: Sat, 13 Feb 2016 22:22:37 +0100 Subject: [PATCH 15/17] Add tests for `rust_version` --- .travis.yml | 1 + test/segments/rust_version.spec | 40 +++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) create mode 100755 test/segments/rust_version.spec diff --git a/.travis.yml b/.travis.yml index e9afcc29..f42835ec 100644 --- a/.travis.yml +++ b/.travis.yml @@ -22,4 +22,5 @@ script: - test/functions/utilities.spec - test/functions/colors.spec - test/segments/dir.spec + - test/segments/rust_version.spec diff --git a/test/segments/rust_version.spec b/test/segments/rust_version.spec new file mode 100755 index 00000000..49b06bcf --- /dev/null +++ b/test/segments/rust_version.spec @@ -0,0 +1,40 @@ +#!/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() { + export TERM="xterm-256color" + # Load Powerlevel9k + source powerlevel9k.zsh-theme +} + +function mockRust() { + echo 'rustc 0.4.1a-alpha' +} + +function testRust() { + alias rustc=mockRust + POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(rust_version) + + assertEquals "%K{208} %F{black}Rust 0.4.1a-alpha %k%F{208}%f " "$(build_left_prompt)" + + unset POWERLEVEL9K_LEFT_PROMPT_ELEMENTS + unalias rustc +} + +function testRustPrintsNothingIfRustIsNotAvailable() { + alias rustc=noRust + POWERLEVEL9K_CUSTOM_WORLD='echo world' + POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(custom_world rust_version) + + assertEquals "%K{white} %F{black}world %k%F{white}%f " "$(build_left_prompt)" + + unset POWERLEVEL9K_LEFT_PROMPT_ELEMENTS + unset POWERLEVEL9K_CUSTOM_WORLD + unalias rustc +} + +source shunit2/source/2.1/src/shunit2 From 3407494d7d0560dfb13c8ecf3e4f1835cb8c3181 Mon Sep 17 00:00:00 2001 From: Dominik Ritter Date: Sat, 13 Feb 2016 22:34:11 +0100 Subject: [PATCH 16/17] Add tests for `go_version` --- .travis.yml | 1 + test/segments/go_version.spec | 40 +++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) create mode 100755 test/segments/go_version.spec diff --git a/.travis.yml b/.travis.yml index f42835ec..71aff690 100644 --- a/.travis.yml +++ b/.travis.yml @@ -23,4 +23,5 @@ script: - test/functions/colors.spec - test/segments/dir.spec - test/segments/rust_version.spec + - test/segments/go_version.spec diff --git a/test/segments/go_version.spec b/test/segments/go_version.spec new file mode 100755 index 00000000..aa9f625d --- /dev/null +++ b/test/segments/go_version.spec @@ -0,0 +1,40 @@ +#!/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() { + export TERM="xterm-256color" + # Load Powerlevel9k + source powerlevel9k.zsh-theme +} + +function mockGo() { + echo 'go version go1.5.3 darwin/amd64' +} + +function testGo() { + alias go=mockGo + POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(go_version) + + assertEquals "%K{green} %F{255}go1.5.3 %k%F{green}%f " "$(build_left_prompt)" + + unset POWERLEVEL9K_LEFT_PROMPT_ELEMENTS + unalias go +} + +function testGoSegmentPrintsNothingIfGoIsNotAvailable() { + alias go=noGo + POWERLEVEL9K_CUSTOM_WORLD='echo world' + POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(custom_world go_version) + + assertEquals "%K{white} %F{black}world %k%F{white}%f " "$(build_left_prompt)" + + unset POWERLEVEL9K_LEFT_PROMPT_ELEMENTS + unset POWERLEVEL9K_CUSTOM_WORLD + unalias go +} + +source shunit2/source/2.1/src/shunit2 From 7efc6e41873e6c329f3b66e43ec853a85f0ad5e9 Mon Sep 17 00:00:00 2001 From: Dominik Ritter Date: Wed, 17 Feb 2016 18:41:46 +0100 Subject: [PATCH 17/17] Fix go test If no GO is available, there should be no output. --- powerlevel9k.zsh-theme | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/powerlevel9k.zsh-theme b/powerlevel9k.zsh-theme index 76ff5497..59733acd 100755 --- a/powerlevel9k.zsh-theme +++ b/powerlevel9k.zsh-theme @@ -471,7 +471,7 @@ prompt_dir() { # GO prompt prompt_go_version() { local go_version - go_version=$(go version 2>&1 | sed -E "s/.*(go[0-9.]*).*/\1/") + go_version=$(go version 2>/dev/null | sed -E "s/.*(go[0-9.]*).*/\1/") if [[ -n "$go_version" ]]; then "$1_prompt_segment" "$0" "$2" "green" "255" "$go_version"