scm_spec.sh (12067B)
1 # pashage - age-backed POSIX password manager 2 # Copyright (C) 2024 Natasha Kerensikova 3 # 4 # This program is free software; you can redistribute it and/or 5 # modify it under the terms of the GNU General Public License 6 # as published by the Free Software Foundation; either version 2 7 # of the License, or (at your option) any later version. 8 # 9 # This program is distributed in the hope that it will be useful, 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 # GNU General Public License for more details. 13 # 14 # You should have received a copy of the GNU General Public License 15 # along with this program; if not, write to the Free Software 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 17 18 # This test file fully covers all SCM functions, 19 # both integrated with git and without a repository. 20 21 Describe 'Integrated SCM Functions' 22 Include src/pashage.sh 23 if [ "${SHELLSPEC_SHELL_TYPE}" = sh ]; then 24 Set 'errexit:on' 'nounset:on' 25 else 26 Set 'errexit:on' 'nounset:on' 'pipefail:on' 27 fi 28 PREFIX="${SHELLSPEC_WORKDIR}/repo" 29 30 git() { @git "$@"; } 31 32 git_log() { 33 @git -C "${PREFIX}" log --format='%s' >|"${SHELLSPEC_WORKDIR}/git-log.txt" 34 } 35 36 git_status() { 37 @git -C "${PREFIX}" status --porcelain \ 38 >|"${SHELLSPEC_WORKDIR}/git-status.txt" 39 } 40 41 setup() { 42 @git init -q -b main "${PREFIX}" 43 @git -C "${PREFIX}" config --local user.name 'Test User' 44 @git -C "${PREFIX}" config --local user.email 'test@example.com' 45 @mkdir "${PREFIX}/subdir" 46 %putsn data >"${PREFIX}/subdir/file.txt" 47 @git -C "${PREFIX}" add subdir/file.txt 48 @git -C "${PREFIX}" commit -m 'Setup a file' >/dev/null 49 } 50 51 cleanup() { 52 @rm -rf "${PREFIX}" 53 } 54 55 BeforeEach setup 56 AfterEach cleanup 57 58 Describe 'scm_add' 59 It 'adds an untracked file' 60 testcase() { 61 %putsn other-data >"${PREFIX}/untracked.txt" 62 scm_add untracked.txt 63 } 64 When call testcase 65 The status should be success 66 The output should be blank 67 The result of function git_status should be successful 68 The contents of file "${SHELLSPEC_WORKDIR}/git-status.txt" \ 69 should equal 'A untracked.txt' 70 End 71 72 It 'adds changes to a tracked file' 73 testcase() { 74 %putsn other-data >|"${PREFIX}/subdir/file.txt" 75 scm_add subdir/file.txt 76 } 77 When call testcase 78 The status should be success 79 The output should be blank 80 The result of function git_status should be successful 81 The contents of file "${SHELLSPEC_WORKDIR}/git-status.txt" \ 82 should equal 'M subdir/file.txt' 83 End 84 End 85 86 Describe 'scm_begin' 87 It 'is successful on a clean repository' 88 When call scm_begin 89 The output should be blank 90 The status should be successful 91 End 92 93 It 'aborts when an untracked file exists' 94 testcase() { 95 %putsn other-data >"${PREFIX}/untracked.txt" 96 scm_begin 97 } 98 When run testcase 99 The status should equal 1 100 The error should equal 'There are already pending changes.' 101 End 102 103 It 'aborts when a tracked file is modified' 104 testcase() { 105 %putsn other-data >|"${PREFIX}/subdir/file.txt" 106 scm_begin 107 } 108 When run testcase 109 The status should equal 1 110 The error should equal 'There are already pending changes.' 111 End 112 113 It 'aborts when there are uncommitted changes' 114 testcase() { 115 %putsn other-data >|"${PREFIX}/subdir/file.txt" 116 scm_add subdir/file.txt 117 scm_begin 118 } 119 When run testcase 120 The status should equal 1 121 The error should equal 'There are already pending changes.' 122 End 123 End 124 125 Describe 'scm_commit' 126 It 'commits a new file' 127 testcase() { 128 scm_begin 129 %putsn other-data >"${PREFIX}/new.txt" 130 scm_add new.txt 131 scm_commit 'New file' 132 } 133 expected_log() { %text 134 #|New file 135 #|Setup a file 136 } 137 When call testcase 138 The output should be blank 139 The status should be successful 140 The result of function git_log should be successful 141 The contents of file "${SHELLSPEC_WORKDIR}/git-log.txt" \ 142 should equal "$(expected_log)" 143 End 144 145 It 'does nothing without scm_add' 146 testcase() { 147 scm_begin 148 %putsn other-data >"${PREFIX}/new.txt" 149 scm_commit 'Nothing' 150 } 151 When call testcase 152 The output should be blank 153 The status should be successful 154 The result of function git_log should be successful 155 The contents of file "${SHELLSPEC_WORKDIR}/git-log.txt" \ 156 should equal "Setup a file" 157 The result of function git_status should be successful 158 The contents of file "${SHELLSPEC_WORKDIR}/git-status.txt" \ 159 should equal "?? new.txt" 160 End 161 End 162 163 Describe 'scm_cp' 164 cp() { @cp "$@"; } 165 166 It 'creates and adds a file' 167 When call scm_cp subdir/file.txt file-copy.txt 168 The status should be success 169 The output should be blank 170 The contents of file "${PREFIX}/subdir/file.txt" should equal 'data' 171 The contents of file "${PREFIX}/file-copy.txt" should equal 'data' 172 The result of function git_status should be successful 173 The contents of file "${SHELLSPEC_WORKDIR}/git-status.txt" \ 174 should equal "A file-copy.txt" 175 End 176 177 It 'copies and adds a directory recursively' 178 When call scm_cp subdir newdir 179 The status should be success 180 The output should be blank 181 The contents of file "${PREFIX}/subdir/file.txt" should equal 'data' 182 The contents of file "${PREFIX}/newdir/file.txt" should equal 'data' 183 The result of function git_status should be successful 184 The contents of file "${SHELLSPEC_WORKDIR}/git-status.txt" \ 185 should equal "A newdir/file.txt" 186 End 187 End 188 189 Describe 'scm_del' 190 rm() { @rm "$@"; } 191 192 It 'deletes a file' 193 When call scm_del subdir/file.txt 194 The status should be success 195 The output should be blank 196 The file "${PREFIX}/subdir/file.txt" should not be exist 197 The result of function git_status should be successful 198 The contents of file "${SHELLSPEC_WORKDIR}/git-status.txt" \ 199 should equal "D subdir/file.txt" 200 End 201 202 It 'deletes a directory recursively' 203 When call scm_del subdir 204 The status should be success 205 The output should be blank 206 The directory "${PREFIX}/subdir" should not be exist 207 The result of function git_status should be successful 208 The contents of file "${SHELLSPEC_WORKDIR}/git-status.txt" \ 209 should equal "D subdir/file.txt" 210 End 211 End 212 213 Describe 'scm_mv' 214 mv() { @mv "$@"; } 215 216 It 'moves a file and records the move' 217 When call scm_mv subdir/file.txt file.txt 218 The status should be success 219 The output should be blank 220 The file "${PREFIX}/subdir/file.txt" should not be exist 221 The contents of file "${PREFIX}/file.txt" should equal 'data' 222 The result of function git_status should be successful 223 The contents of file "${SHELLSPEC_WORKDIR}/git-status.txt" \ 224 should equal 'R subdir/file.txt -> file.txt' 225 End 226 227 It 'moves a directory recursively and records the move' 228 When call scm_mv subdir newdir 229 The status should be success 230 The output should be blank 231 The directory "${PREFIX}/subdir" should not be exist 232 The contents of file "${PREFIX}/newdir/file.txt" should equal 'data' 233 The result of function git_status should be successful 234 The contents of file "${SHELLSPEC_WORKDIR}/git-status.txt" \ 235 should equal 'R subdir/file.txt -> newdir/file.txt' 236 End 237 End 238 239 Describe 'scm_rm' 240 rm() { @rm "$@"; } 241 242 It 'removes a file' 243 When call scm_rm subdir/file.txt 244 The status should be success 245 The output should be blank 246 The file "${PREFIX}/subdir/file.txt" should not be exist 247 The result of function git_status should be successful 248 The contents of file "${SHELLSPEC_WORKDIR}/git-status.txt" \ 249 should equal 'D subdir/file.txt' 250 End 251 252 It 'removes a directory recursively' 253 When call scm_rm subdir 254 The status should be success 255 The output should be blank 256 The directory "${PREFIX}/subdir" should not be exist 257 The result of function git_status should be successful 258 The contents of file "${SHELLSPEC_WORKDIR}/git-status.txt" \ 259 should equal 'D subdir/file.txt' 260 End 261 End 262 End 263 264 Describe 'Integrated SCM Functions without SCM' 265 Include src/pashage.sh 266 if [ "${SHELLSPEC_SHELL_TYPE}" = sh ]; then 267 Set 'errexit:on' 'nounset:on' 268 else 269 Set 'errexit:on' 'nounset:on' 'pipefail:on' 270 fi 271 PREFIX="${SHELLSPEC_WORKDIR}/repo" 272 273 setup() { 274 @mkdir -p "${PREFIX}/subdir" 275 %putsn data >"${PREFIX}/subdir/file.txt" 276 } 277 278 cleanup() { 279 @rm -rf "${PREFIX}" 280 } 281 282 BeforeEach setup 283 AfterEach cleanup 284 285 Describe 'scm_add' 286 It 'does nothing' 287 When call scm_add untracked.txt 288 The output should be blank 289 The status should be successful 290 End 291 End 292 293 Describe 'scm_begin' 294 It 'does nothing' 295 When call scm_begin 296 The output should be blank 297 The status should be successful 298 End 299 300 It 'does nothing even when an untracked file exists' 301 testcase() { 302 %putsn other-data >"${PREFIX}/untracked.txt" 303 scm_begin 304 } 305 When run testcase 306 The output should be blank 307 The status should be successful 308 End 309 End 310 311 Describe 'scm_commit' 312 It 'does nothing even with a new file added' 313 testcase() { 314 scm_begin 315 %putsn other-data >"${PREFIX}/new.txt" 316 scm_add new.txt 317 scm_commit 'New file' 318 } 319 When call testcase 320 The output should be blank 321 The status should be successful 322 End 323 End 324 325 Describe 'scm_cp' 326 cp() { @cp "$@"; } 327 328 It 'creates a file' 329 When call scm_cp subdir/file.txt file-copy.txt 330 The status should be success 331 The output should be blank 332 The contents of file "${PREFIX}/subdir/file.txt" should equal 'data' 333 The contents of file "${PREFIX}/file-copy.txt" should equal 'data' 334 End 335 336 It 'copies a directory recursively' 337 When call scm_cp subdir newdir 338 The status should be success 339 The output should be blank 340 The contents of file "${PREFIX}/subdir/file.txt" should equal 'data' 341 The contents of file "${PREFIX}/newdir/file.txt" should equal 'data' 342 End 343 End 344 345 Describe 'scm_del' 346 It 'does nothing with a file' 347 When call scm_del subdir/file.txt 348 The status should be success 349 The output should be blank 350 The contents of file "${PREFIX}/subdir/file.txt" should equal 'data' 351 End 352 353 It 'does nothing with a directory' 354 When call scm_del subdir 355 The status should be success 356 The output should be blank 357 The contents of file "${PREFIX}/subdir/file.txt" should equal 'data' 358 End 359 End 360 361 Describe 'scm_mv' 362 mv() { @mv "$@"; } 363 364 It 'moves a file' 365 When call scm_mv subdir/file.txt file.txt 366 The status should be success 367 The output should be blank 368 The file "${PREFIX}/subdir/file.txt" should not be exist 369 The contents of file "${PREFIX}/file.txt" should equal 'data' 370 End 371 372 It 'moves a directory recursively' 373 When call scm_mv subdir newdir 374 The status should be success 375 The output should be blank 376 The directory "${PREFIX}/subdir" should not be exist 377 The contents of file "${PREFIX}/newdir/file.txt" should equal 'data' 378 End 379 End 380 381 Describe 'scm_rm' 382 rm() { @rm "$@"; } 383 384 It 'removes a file' 385 When call scm_rm subdir/file.txt 386 The status should be success 387 The output should be blank 388 The file "${PREFIX}/subdir/file.txt" should not be exist 389 End 390 391 It 'removes a directory recursively' 392 When call scm_rm subdir 393 The status should be success 394 The output should be blank 395 The directory "${PREFIX}/subdir" should not be exist 396 End 397 End 398 End