|
| 1 | +#!/usr/bin/env bash |
| 2 | +source ./bash-utils-config |
| 3 | + |
| 4 | +setup() { |
| 5 | + configFileName='.bash-utils-rc' |
| 6 | + globalConfigFile="$HOME/$configFileName" |
| 7 | + printf "createFileComponent=0\n someVar=test" > "$globalConfigFile" |
| 8 | + printf "createFileComponent=1\n" > ./"$configFileName" |
| 9 | +} |
| 10 | + |
| 11 | +teardown() { |
| 12 | + if [ -f "$globalConfigFile" ]; then |
| 13 | + rm "$globalConfigFile" |
| 14 | + fi |
| 15 | + |
| 16 | + if [ -f "./$configFileName" ]; then |
| 17 | + rm ./"$configFileName" |
| 18 | + fi |
| 19 | +} |
| 20 | + |
| 21 | +# readGlobalConfig test cases |
| 22 | +@test "[bash-utils-config] readGlobalConfig: should read the variables from the global config file" { |
| 23 | + readGlobalConfig |
| 24 | + |
| 25 | + [ "$createFileComponent" -eq 0 ] |
| 26 | +} |
| 27 | + |
| 28 | +# readLocalConfig test cases |
| 29 | +@test "[bash-utils-config] readLocalConfig: should read the variables from the local config file" { |
| 30 | + readLocalConfig |
| 31 | + |
| 32 | + [ "$createFileComponent" -eq 1 ] |
| 33 | +} |
| 34 | + |
| 35 | +# findLocalConfig test cases |
| 36 | +@test "[bash-utils-config] findLocalConfig: should find the local config file" { |
| 37 | + run findLocalConfig |
| 38 | + configFile="$PWD"/.bash-utils-rc |
| 39 | + |
| 40 | + [ "$output" = "$configFile" ] |
| 41 | +} |
| 42 | + |
| 43 | +# editConfigFile |
| 44 | +@test "[bash-utils-config] editConfigFile: should open a config file in a default editor" { |
| 45 | + EDITOR=echo |
| 46 | + run editConfigFile |
| 47 | + expectedConfigFile="$PWD"/.bash-utils-rc |
| 48 | + |
| 49 | + [ "$output" = "$expectedConfigFile" ] |
| 50 | +} |
| 51 | + |
| 52 | +@test "[bash-utils-config] editConfigFile: should throw an error if a local config file doesn't exist" { |
| 53 | + configFileName=abracadabre |
| 54 | + run editConfigFile |
| 55 | + [ "$status" -eq 1 ] |
| 56 | + |
| 57 | + value="${csi}31mA local config file doesn't exist"$'\n'"${csi}m" |
| 58 | + [ "$output" = "$value" ] |
| 59 | +} |
| 60 | + |
| 61 | +# editConfigFile |
| 62 | +@test "[bash-utils-config] createConfigFile: should create a config file and open it in a default editor" { |
| 63 | + EDITOR=cat |
| 64 | + expectedConfigFile="$PWD"/.bash-utils-rc |
| 65 | + |
| 66 | + rm "$expectedConfigFile" |
| 67 | + |
| 68 | + [ ! -f "$expectedConfigFile" ] |
| 69 | + |
| 70 | + run createConfigFile |
| 71 | + |
| 72 | + [ -f "$expectedConfigFile" ] |
| 73 | + [ "${lines[0]}" = 'bashUtilsConfig=example' ] |
| 74 | +} |
| 75 | + |
| 76 | +@test "[bash-utils-config] createConfigFile: should throw an error if a local config file already exists" { |
| 77 | + expectedOutput="${csi}31mA file ${PWD}/.bash-utils-rc already exists"$'\n'"${csi}m" |
| 78 | + |
| 79 | + run createConfigFile |
| 80 | + |
| 81 | + [ "$status" -eq 1 ] |
| 82 | + [ "$output" = "$expectedOutput" ] |
| 83 | +} |
| 84 | + |
| 85 | +@test "[bash-utils-config] createConfigFile: should create a config file in a given directory and open it in a default editor" { |
| 86 | + EDITOR=cat |
| 87 | + dir="$HOME"/"$RANDOM" |
| 88 | + |
| 89 | + mkdir "$dir" |
| 90 | + |
| 91 | + expectedConfigFile="$dir"/.bash-utils-rc |
| 92 | + |
| 93 | + [ ! -f "$expectedConfigFile" ] |
| 94 | + |
| 95 | + run createConfigFile "$dir" |
| 96 | + |
| 97 | + [ -f "$expectedConfigFile" ] |
| 98 | + [ "${lines[0]}" = 'bashUtilsConfig=example' ] |
| 99 | + |
| 100 | + rm -rf "$dir" |
| 101 | +} |
| 102 | + |
| 103 | +@test "[bash-utils-config] createConfigFile: should throw an error if a given directory doesn't exist" { |
| 104 | + expectedOutput="${csi}31mA directory /abracadabra doesn't exist"$'\n'"${csi}m" |
| 105 | + |
| 106 | + run createConfigFile /abracadabra |
| 107 | + |
| 108 | + [ "$status" -eq 1 ] |
| 109 | + [ "$output" = "$expectedOutput" ] |
| 110 | +} |
| 111 | + |
| 112 | +@test "[bash-utils-config] deleteConfigFile: should delete a config file if it exists" { |
| 113 | + expectedConfigFile="$PWD"/.bash-utils-rc |
| 114 | + |
| 115 | + [ -f "$expectedConfigFile" ] |
| 116 | + |
| 117 | + run deleteConfigFile |
| 118 | + |
| 119 | + [ ! -f "$expectedConfigFile" ] |
| 120 | +} |
| 121 | + |
| 122 | +# readConfig test cases |
| 123 | +@test "[bash-utils-config] readConfig: should read the variables from the global and the local config files" { |
| 124 | + readConfig |
| 125 | + |
| 126 | + [ "$createFileComponent" -eq 1 ] |
| 127 | + [ "$someVar" = test ] |
| 128 | +} |
| 129 | + |
| 130 | +# bash-utils-config test cases |
| 131 | +@test "[bash-utils-config] create: should proxy its arguments to the createConfigFile function" { |
| 132 | + EDITOR=cat |
| 133 | + expectedConfigFile="$PWD"/.bash-utils-rc |
| 134 | + |
| 135 | + rm "$expectedConfigFile" |
| 136 | + |
| 137 | + [ ! -f "$expectedConfigFile" ] |
| 138 | + |
| 139 | + run bash-utils-config create |
| 140 | + |
| 141 | + [ -f "$expectedConfigFile" ] |
| 142 | + [ "${lines[0]}" = 'bashUtilsConfig=example' ] |
| 143 | +} |
| 144 | + |
| 145 | +@test "[bash-utils-config] bash-utils-config edit: should proxy its arguments to the editConfigFile function" { |
| 146 | + EDITOR=echo |
| 147 | + run bash-utils-config edit |
| 148 | + expectedConfigFile="$PWD"/.bash-utils-rc |
| 149 | + |
| 150 | + [ "$output" = "$expectedConfigFile" ] |
| 151 | +} |
| 152 | + |
| 153 | +@test "[bash-utils-config] bash-utils-config read: should proxy its arguments to the readConfigFile function" { |
| 154 | + bash-utils-config read |
| 155 | + |
| 156 | + [ "$createFileComponent" -eq 1 ] |
| 157 | +} |
| 158 | + |
| 159 | +@test "[bash-utils-config] bash-utils-config delete: should proxy its arguments to the deleteConfigFile function" { |
| 160 | + expectedConfigFile="$PWD"/.bash-utils-rc |
| 161 | + |
| 162 | + [ -f "$expectedConfigFile" ] |
| 163 | + |
| 164 | + run bash-utils-config delete |
| 165 | + |
| 166 | + [ ! -f "$expectedConfigFile" ] |
| 167 | +} |
| 168 | + |
| 169 | +@test "[bash-utils-config] bash-utils-config *: should exit with an error message if an unknow command was given" { |
| 170 | + run bash-utils-config something |
| 171 | + |
| 172 | + [ "$status" -eq 2 ] |
| 173 | + value="${csi}31mAn unknown command bash-utils-config something"$'\n'"${csi}m" |
| 174 | + [ "$output" = "$value" ] |
| 175 | +} |
0 commit comments