File: configm_test.sh

package info (click to toggle)
kworkflow 20191112-1.2
  • links: PTS
  • area: main
  • in suites: bookworm, sid, trixie
  • size: 2,836 kB
  • sloc: perl: 7,354; sh: 2,397; ansic: 80; python: 44; makefile: 38
file content (383 lines) | stat: -rwxr-xr-x 10,871 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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
#!/bin/bash

. ./src/config_manager.sh --source-only
. ./tests/utils --source-only

LS_TITLE="Name\t\tDescription"
COMMAND_MSG_UNKNOWN="Unknown option"
COMMAND_MSG_INVALID_ARG="Invalid argument"
COMMAND_NO_SUCH_FILE="No such file or directory"

readonly YES_FORCE="1"
readonly NO_FORCE="0"

readonly CONTENT="The content"

readonly NAME_1="test_save_1"
readonly NAME_2="test_save_2"

readonly DESCRIPTION_1="This is the first description"
readonly DESCRIPTION_2="Hi, I'm the second description"

readonly LS_NO_FILES="There's no tracked .config file"

function suite
{
  suite_addTest "testExecuteConfigManagerSAVEfails"
  suite_addTest "testSaveConfigFileCHECK_CONFIGfails"
  suite_addTest "testSaveConfigFileCHECK_CONFIGS_DIRECTORY"
  suite_addTest "testSaveConfigFileCHECK_SAVED_CONFIG_FILE"
  suite_addTest "testSaveConfigFileCHECK_DESCRIPTION"
  suite_addTest "testSaveConfigFileCHECK_GIT_SAVE_SCHEMA"
  suite_addTest "testSaveConfigFileCHECK_FORCE"
  suite_addTest "testListConfigsCHECK_NO_CONFIGS"
  suite_addTest "testListConfigsOUTPUT"
  suite_addTest "testGetOperationWithForce"
  suite_addTest "testGetOperationThatShouldFail"
  suite_addTest "testRemoveOperationThatShouldFail"
  suite_addTest "testRemoveOperation"
}

function setupConfigm()
{
  local -r test_path="tests/.tmp"
  local -r current_path=$PWD

  rm -rf $test_path

  mkdir -p $test_path
  cd $test_path
  touch .config
  echo $CONTENT > .config
  cd $current_path
}

function tearDownConfigm()
{
  local -r test_path="tests/.tmp"

  rm -rf $test_path
}

function test_expected_string()
{
  local msg="$1"
  local expected="$2"
  local target="$3"

  assertEquals "$msg" "$target" "$expected"
}

function testExecuteConfigManagerSAVEfails
{
  local msg_prefix=" --save"

  ret=$(execute_config_manager --save)
  test_expected_string "$msg_prefix" "$COMMAND_MSG_INVALID_ARG" "$ret"

  ret=$(execute_config_manager --save --lala)
  test_expected_string "$msg_prefix --lala" "$COMMAND_MSG_INVALID_ARG" "$ret"

  ret=$(execute_config_manager --save -n)
  test_expected_string "$msg_prefix -n" "$COMMAND_MSG_INVALID_ARG" "$ret"

  ret=$(execute_config_manager --save -d)
  test_expected_string "$msg_prefix -d" "$COMMAND_MSG_INVALID_ARG" "$ret"

  ret=$(execute_config_manager --save -n -d)
  test_expected_string "$msg_prefix -n -d" "$COMMAND_MSG_INVALID_ARG" "$ret"

  ret=$(execute_config_manager --save -n -lulu)
  test_expected_string "$msg_prefix -n -lulu" "$COMMAND_MSG_INVALID_ARG" "$ret"

  ret=$(execute_config_manager --save -d)
  test_expected_string "$msg_prefix -d" "$COMMAND_MSG_INVALID_ARG" "$ret"

  ret=$(execute_config_manager --save -d "lalala and xpto")
  test_expected_string "$msg_prefix -d" "$COMMAND_MSG_INVALID_ARG" "$ret"

  ret=$(execute_config_manager --save -f)
  test_expected_string "$msg_prefix -f" "$COMMAND_MSG_INVALID_ARG" "$ret"
}

function testSaveConfigFileCHECK_CONFIGfails()
{
  local -r test_path="tests/.tmp"
  local current_path=$PWD
  local force=0
  local ret=0

  # Prepare teste
  setupConfigm

  # Test without config file -> should fail
  cd $test_path
  rm -f .config
  ret=$(save_config_file $NO_FORCE $NAME_1 "$DESCRIPTION_1")
  assertEquals "No .config file should return ENOENT" "$?" "2"

  # Test with different name
  touch .configuration
  ret=$(save_config_file $NO_FORCE $NAME_1 "$DESCRIPTION_1")
  assertEquals "Should return ENOENT, because '.config' != '.configuration'" "$?" "2"
  rm .configuration

  cd $current_path
  tearDownConfigm
}

function testSaveConfigFileCHECK_CONFIGS_DIRECTORY()
{
  local -r test_path="tests/.tmp"
  local current_path=$PWD
  config_files_path=$current_path/$test_path

  setupConfigm

  # There's no configs yet, initialize it
  cd $test_path
  ret=$(save_config_file $NO_FORCE $NAME_1 "$DESCRIPTION_1")
  cd $current_path

  # Check if all the expected files were created
  assertTrue "The configs dir was not created" '[[ -d $config_files_path/configs ]]'
  assertTrue "The repository configs does not have .git" '[[ -d $config_files_path/configs/.git ]]'
  assertTrue "The metadata dir is not available" '[[ -d $config_files_path/configs/metadata ]]'
  assertTrue "The configs dir is not available" '[[ -d $config_files_path/configs/configs ]]'

  tearDownConfigm
}

function testSaveConfigFileCHECK_SAVED_CONFIG_FILE()
{
  local -r test_path="tests/.tmp"
  local current_path=$PWD
  local ret=0
  config_files_path=$current_path/$test_path

  setupConfigm

  # There's no configs yet, initialize it
  cd $test_path
  ret=$(save_config_file $NO_FORCE $NAME_1 "$DESCRIPTION_1")
  cd $current_path

  assertTrue "Failed to find $NAME_1" '[[ -f $config_files_path/configs/configs/$NAME_1 ]]'
  assertTrue "Failed the metadata related to $NAME_1" '[[ -f $config_files_path/configs/metadata/$NAME_1 ]]'

  cd $test_path
  ret=$(save_config_file $NO_FORCE $NAME_2)
  cd $current_path

  assertTrue "Failed to find $NAME_2" '[[ -f $config_files_path/configs/configs/$NAME_2 ]]'
  assertTrue "Failed the metadata related to $NAME_2" '[[ -f $config_files_path/configs/metadata/$NAME_2 ]]'

  local tmp=$(cat $config_files_path/configs/configs/$NAME_2)
  assertTrue "Content in the file does not match" '[[ $tmp = $CONTENT ]]'

  tearDownConfigm
}

function testSaveConfigFileCHECK_DESCRIPTION()
{
  local -r test_path="tests/.tmp"
  local current_path=$PWD
  local ret=0
  config_files_path=$current_path/$test_path

  setupConfigm

  # There's no configs yet, initialize it
  cd $test_path
  ret=$(save_config_file $NO_FORCE $NAME_1 "$DESCRIPTION_1")
  cd $current_path

  local tmp=$(cat $config_files_path/configs/metadata/$NAME_1)
  assertTrue "The description content for $NAME_1 does not match" '[[ $tmp = $DESCRIPTION_1 ]]'

  cd $test_path
  ret=$(save_config_file $NO_FORCE $NAME_2 "$DESCRIPTION_2")
  cd $current_path

  tmp=$(cat $config_files_path/configs/metadata/$NAME_2)
  assertTrue "The description content for $NAME_2 does not match" '[[ $tmp = $DESCRIPTION_2 ]]'

  tearDownConfigm
}

function testSaveConfigFileCHECK_GIT_SAVE_SCHEMA()
{
  local -r test_path="tests/.tmp"
  local current_path=$PWD
  local ret=0
  config_files_path=$current_path/$test_path

  setupConfigm

  # There's no configs yet, initialize it
  cd $test_path
  ret=$(save_config_file $NO_FORCE $NAME_1 "$DESCRIPTION_1")
  ret=$(save_config_file $NO_FORCE $NAME_2 "$DESCRIPTION_2")
  cd "configs"
  ret=$(git rev-list --all --count)
  cd $current_path

  assertTrue "We expected 2 commits, but we got $ret" '[[ $ret = "2" ]]'

  tearDownConfigm
}

function testSaveConfigFileCHECK_FORCE()
{
  local -r test_path="tests/.tmp"
  local current_path=$PWD
  local ret=0
  config_files_path=$current_path/$test_path

  setupConfigm

  # There's no configs yet, initialize it
  cd $test_path
  ret=$(save_config_file $YES_FORCE $NAME_2 "$DESCRIPTION_2")
  ret=$(save_config_file $YES_FORCE $NAME_2 "$DESCRIPTION_2")
  cd $current_path
  assertTrue "We expected no changes" '[[ $ret =~ Warning ]]'

  tearDownConfigm
}

function testListConfigsCHECK_NO_CONFIGS()
{
  local -r test_path="tests/.tmp"
  local current_path=$PWD
  local ret=0
  config_files_path=$current_path/$test_path

  setupConfigm

  # There's no configs yet, initialize it
  ret=$(list_configs)
  assertTrue "We expected no changes" '[[ $ret =~ $LS_NO_FILES ]]'

  tearDownConfigm
}

function testListConfigsOUTPUT()
{
  local -r test_path="tests/.tmp"
  local current_path=$PWD
  local ret=0
  config_files_path=$current_path/$test_path

  setupConfigm

  # There's no configs yet, initialize it
  cd $test_path
  ret=$(save_config_file $YES_FORCE $NAME_1 "$DESCRIPTION_1")
  ret=$(save_config_file $YES_FORCE $NAME_2 "$DESCRIPTION_2")
  cd $current_path

  # There's no configs yet, initialize it
  ret=$(list_configs)
  assertTrue "We expected 'Name' in the output, but we got $ret" '[[ $ret =~ Name ]]'
  assertTrue "We expected 'Description' in the output, but we got $ret" '[[ $ret =~ Description ]]'
  assertTrue "We expected $NAME_1 in the output, but we got $ret" '[[ $ret =~ $NAME_1 ]]'
  assertTrue "We expected $DESCRIPTION_1 in the output, but we got $ret" '[[ $ret =~ $DESCRIPTION_1 ]]'
  assertTrue "We expected $NAME_2 in the output, but we got $ret" '[[ $ret =~ $NAME_2 ]]'
  assertTrue "We expected $DESCRIPTION_2 in the output, but we got $ret" '[[ $ret =~ $DESCRIPTION_2 ]]'

  tearDownConfigm
}

function testGetOperationThatShouldFail()
{
  local msg_prefix=" --get"

  ret=$(execute_config_manager --get)
  test_expected_string "$msg_prefix" "$COMMAND_MSG_INVALID_ARG" "$ret"

  ret=$(execute_config_manager -get)
  test_expected_string "$msg_prefix" "$COMMAND_MSG_UNKNOWN" "$ret"

  ret=$(execute_config_manager --get something_wrong)
  test_expected_string "$msg_prefix" "$COMMAND_NO_SUCH_FILE: something_wrong" "$ret"
}

function testGetOperationWithForce()
{
  local -r test_path="tests/.tmp"
  local current_path=$PWD
  local ret=0
  config_files_path=$current_path/$test_path

  setupConfigm

  # There's no configs yet, initialize it
  cd $test_path
  ret=$(save_config_file $NO_FORCE $NAME_1 "$DESCRIPTION_1")
  ret=$(save_config_file $NO_FORCE $NAME_2 "$DESCRIPTION_2")
  cd $current_path

  # Case 1: There's no local .config file
  cd $test_path
  rm -f .config
  get_config $NAME_1 1 > /dev/null 2>&1
  ret=$(cat .config)
  cd $current_path

  assertTrue "We expected $CONTENT, but we got $ret" '[[ $ret =~ $CONTENT ]]'

  # Case 2: There's a .config file
  cd $test_path
  get_config $NAME_2 1 > /dev/null 2>&1
  ret=$(cat .config)
  cd $current_path

  assertTrue "We expected $CONTENT, but we got $ret" '[[ $ret =~ $CONTENT ]]'

  tearDownConfigm
}

function testRemoveOperationThatShouldFail()
{
  local msg_prefix=" --rm"

  ret=$(execute_config_manager --rm)
  test_expected_string "$msg_prefix" "$COMMAND_MSG_INVALID_ARG" "$ret"

  ret=$(execute_config_manager -rm)
  test_expected_string "$msg_prefix" "$COMMAND_MSG_UNKNOWN" "$ret"

  ret=$(execute_config_manager --rm something_wrong)
  test_expected_string "$msg_prefix" "$COMMAND_NO_SUCH_FILE: something_wrong" "$ret"
}

function testRemoveOperation()
{
  local -r test_path="tests/.tmp"
  local current_path=$PWD
  local ret=0
  config_files_path=$current_path/$test_path

  setupConfigm

  cd $test_path
  ret=$(save_config_file $NO_FORCE $NAME_1 "$DESCRIPTION_1")
  ret=$(save_config_file $NO_FORCE $NAME_2 "$DESCRIPTION_2")
  ret=$(ls configs/configs -1 | wc -l)
  # Case 1: We should have two files
  assertTrue "We expected , 2 files but got $ret" '[[ $ret = "2" ]]'

  # Case 2: Remove one config file
  remove_config $NAME_1 1  > /dev/null 2>&1
  ret=$(ls configs/configs -1 | wc -l)
  assertTrue "We expected , 1 files but got $ret" '[[ $ret = "1" ]]'

  # Case 2: Remove all config files
  remove_config $NAME_2 1  > /dev/null 2>&1
  assertTrue "We expected no file related to config" '[[ ! -f configs/configs ]]'

  cd $current_path
}

invoke_shunit