File: test_add.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 (279 lines) | stat: -rw-r--r-- 6,452 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
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#!/usr/bin/env bats

load _test_base


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"
}


function teardown {
  uninstall_fixture_key "$TEST_DEFAULT_USER"
  unset_current_state
}


@test "run 'add' normally" {
  # Preparations:
  local filename="$TEST_DEFAULT_FILENAME"
  echo "content" > "$filename"

  run git secret add "$filename"
  [ "$status" -eq 0 ]

  # Ensuring that path mappings was set correctly:
  local path_mappings
  path_mappings=$(_get_secrets_dir_paths_mapping)

  local files_list
  files_list=$(cat "$path_mappings")
  [ "$files_list" = "$filename" ]

  # Cleaning up:
  rm "$filename" ".gitignore"
}


@test "run 'add' with bad arg" {
  local test_file="$TEST_DEFAULT_FILENAME"
  touch "$test_file"
  echo "content" > "$test_file"

  run git secret add -Z "$test_file"
  [ "$status" -ne 0 ]

  rm "$test_file"
}


@test "run 'add' for file ignored by default" {
  local test_file="$TEST_DEFAULT_FILENAME"
  touch "$test_file"
  echo "content" > "$test_file"

  run git secret add "$test_file"
  [ "$status" -eq 0 ]

  rm "$test_file"
}


@test "run 'add' for file ignored with '-i' and '.gitignore' contents" {
  local test_file="$TEST_DEFAULT_FILENAME"
  touch "$test_file"
  echo "content" > "$test_file"

  local quoted_name
  quoted_name=$(printf '%q' "$test_file")

  # add -i is now a no-op (See #225) so this tests that -i does nothing.
  run git secret add -i "$test_file"
  [ "$status" -eq 0 ]

  run file_has_line "$quoted_name" ".gitignore"
  [ "$output" = '0' ]

  local expected=.gitsec/keys/random_seed$'\n'\!\*.sec$'\n'$quoted_name
  echo "$expected" > '.expected'

  [ "$(cmp '.expected' '.gitignore'; echo $?)" -eq 0 ]

  rm "$test_file" '.expected'
}


@test "run 'add' for file ignored by default and with '-i' in subfolder" {
  # This test covers this issue:
  # https://github.com/sobolevn/git-secret/issues/85 task 1

  if [[ "$BATS_RUNNING_FROM_GIT" -eq 1 ]]; then
    # See #334 for more about this
    skip "this test is skipped while 'git commit'"
  fi

  # Preparations:
  local test_dir='test_dir'
  local nested_dir="$test_dir/adding"
  local current_dir="$PWD"

  mkdir -p "$nested_dir"
  cd "$nested_dir"

  local test_file='test_file.auto_ignore'
  touch "$test_file"
  echo "content" > "$test_file"

  # Test commands:
  run git secret add -i "$test_file"
  [ "$status" -eq 0 ]

  [[ -f "$current_dir/.gitignore" ]]
  run file_has_line "$nested_dir/$test_file" "$current_dir/.gitignore"
  [ "$output" = '0' ]

  # .gitignore was not created:
  [[ ! -f ".gitignore" ]]

  # Cleaning up:
  cd "$current_dir"
  rm -r "$test_dir"
}


@test "run 'add' for relative path" {
  if [[ "$BATS_RUNNING_FROM_GIT" -eq 1 ]]; then
    skip "this test is skipped while 'git commit'. See #334"
  fi

  # Preparations:
  local root='test_dir'
  local node="$root/node"
  local sibling="$root/sibling"
  local test_file="$node/$TEST_DEFAULT_FILENAME"
  local current_dir="$PWD"

  mkdir -p "$node"
  mkdir -p "$sibling"

  echo "content" > "$test_file"

  cd "$sibling"

  # Testing:
  run git secret add "../node/$TEST_DEFAULT_FILENAME"
  [ "$status" -eq 0 ]
  [[ "$output" == *"git-secret: 1 item(s) added."* ]]

  # Testing mappings content:
  local path_mappings
  path_mappings=$(_get_secrets_dir_paths_mapping)

  local files_list
  files_list=$(cat "$path_mappings")
  [ "$files_list" = "$test_file" ]

  # Cleaning up:
  cd "$current_dir"
  rm -r "$root"
}


@test "run 'add' for file in subfolder" {
  if [[ "$BATS_RUNNING_FROM_GIT" -eq 1 ]]; then
    # See #334 for more about this
    skip "this test is skipped while 'git commit'"
  fi

  # Preparations:
  local test_file="$TEST_DEFAULT_FILENAME"
  local test_dir='test_dir'

  mkdir -p "$test_dir"
  touch "$test_dir/$test_file"
  echo "content" > "$test_dir/$test_file"

  # Testing:
  run git secret add "$test_dir/$test_file"
  [ "$status" -eq 0 ]
  [[ "$output" == *"git-secret: 1 item(s) added."* ]]

  # Cleaning up:
  rm -r "$test_dir"
}


@test "run 'add' twice for one file" {
  # Preparations:
  local filename="$TEST_DEFAULT_FILENAME"
  echo "content" > "$filename"

  # Testing:
  run git secret add "$filename"
  run git secret add "$filename"
  [ "$status" -eq 0 ]
  [[ "$output" = *"git-secret: 0 item(s) added."* ]]

  # Ensuring that path mappings was set correctly:
  local path_mappings
  path_mappings=$(_get_secrets_dir_paths_mapping)

  local files_list
  files_list=$(cat "$path_mappings")
  [ "$files_list" = "$filename" ]

  # Cleaning up:
  rm "$filename" ".gitignore"
}


@test "run 'add' for multiple files, and test .gitignore contents" {
  # Preparations:
  local filename1="$TEST_DEFAULT_FILENAME"
  echo "content1" > "$filename1"

  local filename2="$TEST_SECOND_FILENAME"
  echo "content2" > "$filename2"

  # Testing:
  run git secret add "$filename1" "$filename2"
  [ "$status" -eq 0 ]
  [[ "$output" = *"git-secret: 2 item(s) added."* ]]   # there may be additional lines too

  # test .gitignore has 4 lines as expected
  local gitignore_linecount
  gitignore_linecount=$(wc -l < .gitignore)
  [ "$gitignore_linecount" -eq 4 ]          # two added by `git secret init`, and one for each `added` file

  # Cleaning up:
  rm "$filename1" "$filename2" ".gitignore"
}

@test "run 'add -v' for multiple files" {
  # Preparations:
  local filename1="$TEST_DEFAULT_FILENAME"
  echo "content1" > "$filename1"

  local filename2="$TEST_SECOND_FILENAME"
  echo "content2" > "$filename2"

  # Testing:
  run git secret add -v "$filename1" "$filename2"

  [ "$status" -eq 0 ]
  [[ "$output" == *"git-secret: adding file: ${TEST_DEFAULT_FILENAME}"* ]]
  [[ "$output" == *"git-secret: adding file: ${TEST_SECOND_FILENAME}"* ]]
  [[ "$output" == *"git-secret: 2 item(s) added."* ]]

  # Cleaning up:
  rm "$filename1" "$filename2" ".gitignore"
}

@test "run 'add' for file with special chars" {
  # Preparations:
  local filename="$TEST_FOURTH_FILENAME"
  echo "content" > "$filename"

  run git secret add "$filename"
  [ "$status" -eq 0 ]

  # Ensuring that path mappings was set correctly:
  local path_mappings
  path_mappings=$(_get_secrets_dir_paths_mapping)

  local files_list
  files_list=$(cat "$path_mappings")
  [ "$files_list" = "$filename" ]

  # Ensuring the file is correctly git-ignored
  run git check-ignore "$filename"
  [ "$status" -eq 0 ]

  # Cleaning up:
  rm "$filename" ".gitignore"
}