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

Commit 7635d4f

Browse files
committed
node_version project-only update docs and tests
1 parent 45b8fb7 commit 7635d4f

File tree

2 files changed

+75
-15
lines changed

2 files changed

+75
-15
lines changed

segments/node_version/README.md

+10
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,16 @@ where you want to show this segment.
1010

1111
## Configuration
1212

13+
### Segment Display
14+
15+
By default, this segment is always shown. You can also choose to have it
16+
displayed only when inside of a Node project by setting
17+
```
18+
P9K_NODE_VERSION_PROJECT_ONLY=true
19+
```
20+
The current directory and its ancestors will be searched for a `project.json`
21+
file, and the segment will only be displayed if one is located before `/`.
22+
1323
### Color Customization
1424

1525
You can change the foreground and background color of this segment by setting

segments/node_version/node_version.spec

+65-15
Original file line numberDiff line numberDiff line change
@@ -5,36 +5,86 @@
55
setopt shwordsplit
66
SHUNIT_PARENT=$0
77

8-
function setUp() {
8+
function oneTimeSetUp() {
99
export TERM="xterm-256color"
10-
local -a P9K_RIGHT_PROMPT_ELEMENTS
11-
P9K_RIGHT_PROMPT_ELEMENTS=()
10+
11+
OLD_DIR="${PWD}"
12+
13+
TEST_BASE_FOLDER="/tmp/powerlevel9k-test"
14+
NODE_VERSION_TEST_FOLDER="${TEST_BASE_FOLDER}/node_version-test"
15+
16+
OLDPATH="${PATH}"
17+
PATH="${NODE_VERSION_TEST_FOLDER}:${PATH}"
18+
19+
P9K_CUSTOM_WORLD='echo world'
20+
21+
node() {
22+
echo "v1.2.3"
23+
}
24+
}
25+
26+
function setUp() {
1227
# Load Powerlevel9k
13-
source powerlevel9k.zsh-theme
14-
source segments/node_version/node_version.p9k
28+
source "${OLD_DIR}/powerlevel9k.zsh-theme"
29+
source "${OLD_DIR}/segments/node_version/node_version.p9k"
30+
31+
P9K_LEFT_PROMPT_ELEMENTS=(node_version)
32+
P9K_RIGHT_PROMPT_ELEMENTS=()
33+
34+
mkdir -p "${NODE_VERSION_TEST_FOLDER}"
35+
cd "${NODE_VERSION_TEST_FOLDER}"
36+
}
37+
38+
function tearDown() {
39+
cd "${OLD_DIR}"
40+
rm -rf "${NODE_VERSION_TEST_FOLDER}"
41+
}
42+
43+
function oneTimeTearDown() {
44+
PATH="${OLDPATH}"
45+
46+
cd "${OLD_DIR}"
47+
rm -rf "${TEST_BASE_FOLDER}"
48+
}
49+
50+
function testNodeVersionSegmentWorks() {
51+
assertEquals "%K{002} %F{015}⬢ %F{015}1.2.3 %k%F{002}%f " "$(__p9k_build_left_prompt)"
1552
}
1653

1754
function testNodeVersionSegmentPrintsNothingWithoutNode() {
18-
local -a P9K_LEFT_PROMPT_ELEMENTS
19-
P9K_LEFT_PROMPT_ELEMENTS=(node_version custom_world)
20-
local P9K_CUSTOM_WORLD='echo world'
55+
P9K_LEFT_PROMPT_ELEMENTS+=(custom_world)
56+
2157
alias node="nonode 2>/dev/null"
2258

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

2561
unalias node
2662
}
2763

28-
function testNodeVersionSegmentWorks() {
29-
local -a P9K_LEFT_PROMPT_ELEMENTS
30-
P9K_LEFT_PROMPT_ELEMENTS=(node_version)
31-
node() {
32-
echo "v1.2.3"
33-
}
64+
function testNodeVersionSegmentProjectOnlyPrintsNothingOutsideProjectDirectory() {
65+
P9K_LEFT_PROMPT_ELEMENTS+=(custom_world)
66+
67+
P9K_NODE_VERSION_PROJECT_ONLY=true
68+
69+
assertEquals "%K{015} %F{000}world %k%F{015}%f " "$(__p9k_build_left_prompt)"
70+
}
71+
72+
function testNodeVersionSegmentProjectOnlyWorksInsideProjectDirectory() {
73+
P9K_NODE_VERSION_PROJECT_ONLY=true
74+
75+
touch ./package.json
3476

3577
assertEquals "%K{002} %F{015}⬢ %F{015}1.2.3 %k%F{002}%f " "$(__p9k_build_left_prompt)"
78+
}
79+
80+
function testNodeVersionSegmentProjectOnlyWorksInsideProjectChildDirectory() {
81+
P9K_NODE_VERSION_PROJECT_ONLY=true
3682

37-
unfunction node
83+
touch ./package.json
84+
mkdir test-child
85+
cd test-child
86+
87+
assertEquals "%K{002} %F{015}⬢ %F{015}1.2.3 %k%F{002}%f " "$(__p9k_build_left_prompt)"
3888
}
3989

4090
source shunit2/shunit2

0 commit comments

Comments
 (0)