aboutsummaryrefslogtreecommitdiff
path: root/test/segments/load.spec
blob: 5867b41955c6e2ac2bbf8d17b6bc20fcec7bd2ec (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#!/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"

  P9K_HOME=$(pwd)
  ### Test specific
  # Create default folder and git init it.
  FOLDER=/tmp/powerlevel9k-test/load-test
  mkdir -p "${FOLDER}"
  cd $FOLDER
}

function tearDown() {
  # Go back to powerlevel9k folder
  cd "${P9K_HOME}"
  # Remove eventually created test-specific folder
  rm -fr "${FOLDER}"
  # At least remove test folder completely
  rm -fr /tmp/powerlevel9k-test
}

function testLoadSegmentWorksOnOsx() {
    sysctl() {
        if [[ "$*" == 'vm.loadavg' ]]; then
            echo "vm.loadavg: { 1,38 1,45 2,16 }";
        fi

        if [[ "$*" == '-n hw.logicalcpu' ]]; then
            echo "4";
        fi
    }

    local POWERLEVEL9K_LOAD_WHICH=1

    # Load Powerlevel9k
    source ${P9K_HOME}/powerlevel9k.zsh-theme
    local OS="OSX" # Fake OSX

    assertEquals "%K{002} %F{000}L %F{000}1.38 " "$(prompt_load left 1 false ${FOLDER})"

    unfunction sysctl
}

function testLoadSegmentWorksOnBsd() {
    sysctl() {
        if [[ "$*" == 'vm.loadavg' ]]; then
            echo "vm.loadavg: { 1,38 1,45 2,16 }";
        fi

        if [[ "$*" == '-n hw.ncpu' ]]; then
            echo "4";
        fi
    }

    local POWERLEVEL9K_LOAD_WHICH=1
    
    # Load Powerlevel9k
    source ${P9K_HOME}/powerlevel9k.zsh-theme
    local OS="BSD" # Fake BSD

    assertEquals "%K{002} %F{000}L %F{000}1.38 " "$(prompt_load left 1 false ${FOLDER})"

    unfunction sysctl
}

function testLoadSegmentWorksOnLinux() {
    # Prepare loadavg
    mkdir proc
    echo "1.38 0.01 0.05 1/87 8641" > proc/loadavg

    alias nproc="echo 4"
    local POWERLEVEL9K_LOAD_WHICH=1

    # Load Powerlevel9k
    source ${P9K_HOME}/powerlevel9k.zsh-theme
    local OS="Linux" # Fake Linux

    assertEquals "%K{002} %F{000}L %F{000}1.38 " "$(prompt_load left 1 false ${FOLDER})"

    unalias nproc
}

# Test normal state. This test is not OS specific.
# We test it as the Linux version, but that
# does not matter here.
function testLoadSegmentNormalState() {
    # Prepare loadavg
    mkdir proc
    echo "1.00 0.01 0.05 1/87 8641" > proc/loadavg

    alias nproc="echo 4"
    local POWERLEVEL9K_LOAD_WHICH=1

    # Load Powerlevel9k
    source ${P9K_HOME}/powerlevel9k.zsh-theme
    local OS="Linux" # Fake Linux

    assertEquals "%K{002} %F{000}L %F{000}1.00 " "$(prompt_load left 1 false ${FOLDER})"

    unalias nproc
}

# Test warning state. This test is not OS specific.
# We test it as the Linux version, but that
# does not matter here.
function testLoadSegmentWarningState() {
    # Prepare loadavg
    mkdir proc
    echo "2.01 0.01 0.05 1/87 8641" > proc/loadavg

    alias nproc="echo 4"
    local POWERLEVEL9K_LOAD_WHICH=1

    # Load Powerlevel9k
    source ${P9K_HOME}/powerlevel9k.zsh-theme
    local OS="Linux" # Fake Linux

    assertEquals "%K{003} %F{000}L %F{000}2.01 " "$(prompt_load left 1 false ${FOLDER})"

    unalias nproc
}

# Test critical state. This test is not OS specific.
# We test it as the Linux version, but that
# does not matter here.
function testLoadSegmentCriticalState() {
    # Prepare loadavg
    mkdir proc
    echo "2.81 0.01 0.05 1/87 8641" > proc/loadavg

    alias nproc="echo 4"
    local POWERLEVEL9K_LOAD_WHICH=1

    # Load Powerlevel9k
    source ${P9K_HOME}/powerlevel9k.zsh-theme
    local OS="Linux" # Fake Linux

    assertEquals "%K{001} %F{000}L %F{000}2.81 " "$(prompt_load left 1 false ${FOLDER})"

    unalias nproc
}

source shunit2/shunit2