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

Commit db3bfbf

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

File tree

2 files changed

+80
-13
lines changed

2 files changed

+80
-13
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

+70-13
Original file line numberDiff line numberDiff line change
@@ -5,36 +5,93 @@
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+
OLDPATH="${PATH}"
12+
PATH="${NODE_VERSION_TEST_FOLDER}:${PATH}"
13+
14+
OLD_DIR="${PWD}"
15+
16+
TEST_BASE_FOLDER="/tmp/powerlevel9k-test"
17+
NODE_VERSION_TEST_FOLDER="${TEST_BASE_FOLDER}/node_version-test"
18+
19+
node() {
20+
echo "v1.2.3"
21+
}
22+
}
23+
24+
function setUp() {
1225
# Load Powerlevel9k
13-
source powerlevel9k.zsh-theme
14-
source segments/node_version/node_version.p9k
26+
source "${OLD_DIR}/powerlevel9k.zsh-theme"
27+
source "${OLD_DIR}/segments/node_version/node_version.p9k"
28+
29+
P9K_LEFT_PROMPT_ELEMENTS=(node_version)
30+
P9K_RIGHT_PROMPT_ELEMENTS=()
31+
32+
mkdir -p "${NODE_VERSION_TEST_FOLDER}"
33+
cd "${NODE_VERSION_TEST_FOLDER}"
34+
}
35+
36+
function tearDown() {
37+
cd "${OLD_DIR}"
38+
rm -rf "${NODE_VERSION_TEST_FOLDER}"
39+
}
40+
41+
function oneTimeTearDown() {
42+
PATH="${OLDPATH}"
43+
44+
cd "${OLD_DIR}"
45+
rm -rf "${TEST_BASE_FOLDER}"
46+
}
47+
48+
function testNodeVersionSegmentWorks() {
49+
P9K_NODE_VERSION_PROJECT_ONLY=false
50+
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
1955
P9K_LEFT_PROMPT_ELEMENTS=(node_version custom_world)
2056
local P9K_CUSTOM_WORLD='echo world'
57+
2158
alias node="nonode 2>/dev/null"
2259

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

2562
unalias node
2663
}
2764

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-
}
65+
function testNodeVersionSegmentProjectOnlyPrintsNothingOutsideProjectDirectory() {
66+
P9K_LEFT_PROMPT_ELEMENTS=(node_version custom_world)
67+
local P9K_CUSTOM_WORLD='echo world'
68+
69+
P9K_NODE_VERSION_PROJECT_ONLY=true
70+
71+
assertEquals "%K{015} %F{000}world %k%F{015}%f " "$(__p9k_build_left_prompt)"
72+
}
73+
74+
function testNodeVersionSegmentProjectOnlyWorksInsideProjectDirectory() {
75+
P9K_NODE_VERSION_PROJECT_ONLY=true
76+
77+
touch ./package.json
3478

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

37-
unfunction node
81+
rm ./package.json
82+
}
83+
84+
function testNodeVersionSegmentProjectOnlyWorksInsideProjectChildDirectory() {
85+
P9K_NODE_VERSION_PROJECT_ONLY=true
86+
87+
touch ./package.json
88+
mkdir test-child
89+
cd test-child
90+
91+
assertEquals "%K{002} %F{015}⬢ %F{015}1.2.3 %k%F{002}%f " "$(__p9k_build_left_prompt)"
92+
93+
cd ..
94+
rm ./package.json
3895
}
3996

4097
source shunit2/shunit2

0 commit comments

Comments
 (0)