Skip to content
This repository has been archived by the owner on Apr 24, 2020. It is now read-only.

Commit

Permalink
node_version project-only update docs and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisBaker97 committed Mar 18, 2019
1 parent 45b8fb7 commit 7635d4f
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 15 deletions.
10 changes: 10 additions & 0 deletions segments/node_version/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,16 @@ where you want to show this segment.

## Configuration

### Segment Display

By default, this segment is always shown. You can also choose to have it
displayed only when inside of a Node project by setting
```
P9K_NODE_VERSION_PROJECT_ONLY=true
```
The current directory and its ancestors will be searched for a `project.json`
file, and the segment will only be displayed if one is located before `/`.

### Color Customization

You can change the foreground and background color of this segment by setting
Expand Down
80 changes: 65 additions & 15 deletions segments/node_version/node_version.spec
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,86 @@
setopt shwordsplit
SHUNIT_PARENT=$0

function setUp() {
function oneTimeSetUp() {
export TERM="xterm-256color"
local -a P9K_RIGHT_PROMPT_ELEMENTS
P9K_RIGHT_PROMPT_ELEMENTS=()

OLD_DIR="${PWD}"

TEST_BASE_FOLDER="/tmp/powerlevel9k-test"
NODE_VERSION_TEST_FOLDER="${TEST_BASE_FOLDER}/node_version-test"

OLDPATH="${PATH}"
PATH="${NODE_VERSION_TEST_FOLDER}:${PATH}"

P9K_CUSTOM_WORLD='echo world'

node() {
echo "v1.2.3"
}
}

function setUp() {
# Load Powerlevel9k
source powerlevel9k.zsh-theme
source segments/node_version/node_version.p9k
source "${OLD_DIR}/powerlevel9k.zsh-theme"
source "${OLD_DIR}/segments/node_version/node_version.p9k"

P9K_LEFT_PROMPT_ELEMENTS=(node_version)
P9K_RIGHT_PROMPT_ELEMENTS=()

mkdir -p "${NODE_VERSION_TEST_FOLDER}"
cd "${NODE_VERSION_TEST_FOLDER}"
}

function tearDown() {
cd "${OLD_DIR}"
rm -rf "${NODE_VERSION_TEST_FOLDER}"
}

function oneTimeTearDown() {
PATH="${OLDPATH}"

cd "${OLD_DIR}"
rm -rf "${TEST_BASE_FOLDER}"
}

function testNodeVersionSegmentWorks() {
assertEquals "%K{002} %F{015}⬢ %F{015}1.2.3 %k%F{002}%f " "$(__p9k_build_left_prompt)"
}

function testNodeVersionSegmentPrintsNothingWithoutNode() {
local -a P9K_LEFT_PROMPT_ELEMENTS
P9K_LEFT_PROMPT_ELEMENTS=(node_version custom_world)
local P9K_CUSTOM_WORLD='echo world'
P9K_LEFT_PROMPT_ELEMENTS+=(custom_world)

alias node="nonode 2>/dev/null"

assertEquals "%K{015} %F{000}world %k%F{015}%f " "$(__p9k_build_left_prompt)"

unalias node
}

function testNodeVersionSegmentWorks() {
local -a P9K_LEFT_PROMPT_ELEMENTS
P9K_LEFT_PROMPT_ELEMENTS=(node_version)
node() {
echo "v1.2.3"
}
function testNodeVersionSegmentProjectOnlyPrintsNothingOutsideProjectDirectory() {
P9K_LEFT_PROMPT_ELEMENTS+=(custom_world)

P9K_NODE_VERSION_PROJECT_ONLY=true

assertEquals "%K{015} %F{000}world %k%F{015}%f " "$(__p9k_build_left_prompt)"
}

function testNodeVersionSegmentProjectOnlyWorksInsideProjectDirectory() {
P9K_NODE_VERSION_PROJECT_ONLY=true

touch ./package.json

assertEquals "%K{002} %F{015}⬢ %F{015}1.2.3 %k%F{002}%f " "$(__p9k_build_left_prompt)"
}

function testNodeVersionSegmentProjectOnlyWorksInsideProjectChildDirectory() {
P9K_NODE_VERSION_PROJECT_ONLY=true

unfunction node
touch ./package.json
mkdir test-child
cd test-child

assertEquals "%K{002} %F{015}⬢ %F{015}1.2.3 %k%F{002}%f " "$(__p9k_build_left_prompt)"
}

source shunit2/shunit2

0 comments on commit 7635d4f

Please sign in to comment.