File: test_remove.bats

package info (click to toggle)
git-secret 0.5.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 2,724 kB
  • sloc: sh: 4,580; makefile: 162; xml: 31; python: 22
file content (113 lines) | stat: -rw-r--r-- 2,671 bytes parent folder | download
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
#!/usr/bin/env bats

load _test_base

FIRST_FILE="$TEST_DEFAULT_FILENAME"
SECOND_FILE="$TEST_SECOND_FILENAME"


function setup {
  install_fixture_key "$TEST_DEFAULT_USER"

  set_state_initial
  set_state_git
  set_state_secret_init
  set_state_secret_tell "$TEST_DEFAULT_USER"
  set_state_secret_add "$FIRST_FILE" "somecontent"
  set_state_secret_add "$SECOND_FILE" "somecontent2"
  set_state_secret_hide
}


function teardown {
  rm "$FIRST_FILE" "$SECOND_FILE"

  uninstall_fixture_key "$TEST_DEFAULT_USER"
  unset_current_state
}


function _has_line {
  local line="$1"

  local path_mappings
  path_mappings=$(_get_secrets_dir_paths_mapping)

  echo "$(grep -q "$line" "$path_mappings"; echo $?)"
}


@test "run 'remove' normally" {
  run git secret remove "$SECOND_FILE"
  [ "$status" -eq 0 ]

  # Test output:
  [[ "$output" == *"removed from index."* ]]
  [[ "$output" == *"ensure that files: [$SECOND_FILE] are now not ignored."* ]]

  # Mapping should not contain the second file:
  [ "$(_has_line "$SECOND_FILE")" -eq 1 ]

  # But the first file must not change:
  [ "$(_has_line "$FIRST_FILE")" -eq 0 ]

  # Both files should be present:
  [ -f "$(_get_encrypted_filename "$FIRST_FILE")" ]
  [ -f "$(_get_encrypted_filename "$SECOND_FILE")" ]
}


@test "run 'remove' with multiple arguments" {
  run git secret remove "$FIRST_FILE" "$SECOND_FILE"
  [ "$status" -eq 0 ]

  [ "$(_has_line "$FIRST_FILE")" -eq 1 ]
  [ "$(_has_line "$SECOND_FILE")" -eq 1 ]

  # Both files should be present:
  [ -f "$(_get_encrypted_filename "$FIRST_FILE")" ]
  [ -f "$(_get_encrypted_filename "$SECOND_FILE")" ]
}


@test "run 'remove' with slashes in filename" {
  # There was a bug with `sed` an slashes:
  # see https://github.com/sobolevn/git-secret/issues/23

  # Preparations:
  local folder="somedir"
  local file_in_folder="$folder/$TEST_THIRD_FILENAME"

  mkdir -p "$folder"
  set_state_secret_add "$file_in_folder" "somecontent3"
  set_state_secret_hide # running hide again to hide new data

  # Now it should remove filename with slashes from the mapping:
  run git secret remove "$file_in_folder"
  [ "$status" -eq 0 ]

  [ "$(_has_line "$file_in_folder")" -eq 1 ]
  [ -f "$(_get_encrypted_filename "$file_in_folder")" ]

  # Cleaning up:
  rm -rf "$folder"
}


@test "run 'remove' with '-c'" {
  set_state_secret_hide

  run git secret remove -c "$SECOND_FILE"
  [ "$status" -eq 0 ]

  [ "$(_has_line "$SECOND_FILE")" -eq 1 ]
  [ -f "$(_get_encrypted_filename "$FIRST_FILE")" ]
  [ ! -f "$(_get_encrypted_filename "$SECOND_FILE")" ]
}


@test "run 'remove' with bad arg" {
  set_state_secret_hide
  run git secret remove -Z "$SECOND_FILE"
  [ "$status" -ne 0 ]
}