File: 59-assert-10-assert_file_owner.bats

package info (click to toggle)
bats-file 0.3.0%2Bgit20230131-gbba751f-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 408 kB
  • sloc: sh: 566; makefile: 2
file content (81 lines) | stat: -rw-r--r-- 2,708 bytes parent folder | download | duplicates (3)
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
#!/usr/bin/env bats

load 'test_helper'
fixtures 'exist'

setup () {
  touch ${TEST_FIXTURE_ROOT}/dir/owner ${TEST_FIXTURE_ROOT}/dir/notowner
  # There is PATH addition to /usr/sbin which won't come by default on bash3
  # on macOS
  chown_path=$(PATH="$PATH:/usr/sbin" command -v chown 2>/dev/null)
  bats_sudo "$chown_path" root ${TEST_FIXTURE_ROOT}/dir/owner
  bats_sudo "$chown_path" daemon ${TEST_FIXTURE_ROOT}/dir/notowner
}

teardown () {
  bats_sudo rm -f ${TEST_FIXTURE_ROOT}/dir/owner ${TEST_FIXTURE_ROOT}/dir/notowner
}

# Correctness
@test 'assert_file_owner() <file>: returns 0 if <file> user root is the owner of the file' {
  local -r owner="root"
  local -r file="${TEST_FIXTURE_ROOT}/dir/owner"
  run assert_file_owner "$owner" "$file"
  [ "$status" -eq 0 ]
  echo ${#lines[@]}
  [ "${#lines[@]}" -eq 0 ]

}

@test 'assert_file_owner() <file>: returns 1 and displays path if <file> user root is not the owner of the file' {
  local -r owner="root"
  local -r file="${TEST_FIXTURE_ROOT}/dir/notowner"
  run assert_file_owner "$owner" "$file"
  [ "$status" -eq 1 ]
  [ "${#lines[@]}" -eq 3 ]
  [ "${lines[0]}" == '-- user root is not the owner of the file --' ]
  [ "${lines[1]}" == "path : $file" ]
  [ "${lines[2]}" == '--' ]
}



# Transforming path
@test 'assert_file_owner() <file>: replace prefix of displayed path' {
  local -r BATSLIB_FILE_PATH_REM="#${TEST_FIXTURE_ROOT}"
  local -r BATSLIB_FILE_PATH_ADD='..'
  local -r owner="root"
  local -r file="${TEST_FIXTURE_ROOT}/dir/notowner"
  run assert_file_owner "$owner" "$file"
  [ "$status" -eq 1 ]
  [ "${#lines[@]}" -eq 3 ]
  [ "${lines[0]}" == '-- user root is not the owner of the file --' ]
  [ "${lines[1]}" == "path : ../dir/notowner" ]
  [ "${lines[2]}" == '--' ]
}

@test 'assert_file_owner() <file>: replace suffix of displayed path' {
  local -r BATSLIB_FILE_PATH_REM='%dir/notowner'
  local -r BATSLIB_FILE_PATH_ADD='..'
  local -r owner="root"
  local -r file="${TEST_FIXTURE_ROOT}/dir/notowner"
  run assert_file_owner "$owner" "$file"
  [ "$status" -eq 1 ]
  [ "${#lines[@]}" -eq 3 ]
  [ "${lines[0]}" == '-- user root is not the owner of the file --' ]
  [ "${lines[1]}" == "path : ${TEST_FIXTURE_ROOT}/.." ]
  [ "${lines[2]}" == '--' ]
}

@test 'assert_file_owner() <file>: replace infix of displayed path' {
  local -r BATSLIB_FILE_PATH_REM='dir/notowner'
  local -r BATSLIB_FILE_PATH_ADD='..'
  local -r owner="root"
  local -r file="${TEST_FIXTURE_ROOT}/dir/notowner"
  run assert_file_owner "$owner" "$file"
  [ "$status" -eq 1 ]
  [ "${#lines[@]}" -eq 3 ]
  [ "${lines[0]}" == '-- user root is not the owner of the file --' ]
  [ "${lines[1]}" == "path : ${TEST_FIXTURE_ROOT}/.." ]
  [ "${lines[2]}" == '--' ]
}