asdf

Форк
0
/
banned_commands.bats 
101 строка · 3.3 Кб
1
#!/usr/bin/env bats
2

3
load test_helpers
4

5
banned_commands=(
6
  # Process substitution isn't POSIX compliant and cause trouble
7
  "<("
8
  # Command isn't included in the Ubuntu packages asdf depends on. Also not
9
  # defined in POSIX
10
  column
11
  # echo isn't consistent across operating systems, and sometimes output can
12
  # be confused with echo flags. printf does everything echo does and more.
13
  echo
14
  # It's best to avoid eval as it makes it easier to accidentally execute
15
  # arbitrary strings
16
  eval
17
  # realpath not available by default on OSX.
18
  realpath
19
  # source isn't POSIX compliant. . behaves the same and is POSIX compliant
20
  # Except in fish, where . is deprecated, and will be removed in the future.
21
  source
22
  # For consistency, [ should be used instead. There is a leading space so 'fail_test', etc. is not matched
23
  ' test'
24
)
25

26
banned_commands_regex=(
27
  # grep -y does not work on alpine and should be "grep -i" either way
28
  "grep.* -y"
29
  # grep -P is not a valid option in OSX.
30
  "grep.* -P"
31
  # Ban grep long commands as they do not work on alpine
32
  "grep[^|]+--\w{2,}"
33
  # readlink -f on OSX behaves differently from readlink -f on other Unix systems
34
  'readlink.+-.*f.+["$]'
35
  # sort --sort-version isn't supported everywhere
36
  "sort.*-V"
37
  "sort.*--sort-versions"
38

39
  # ls often gets used when we want to glob for files that match a pattern
40
  # or when we want to find all files/directories that match a pattern or are
41
  # found in a certain location. Using shell globs is preferred over ls, and
42
  # find is better at locating files that are in a certain location or that
43
  # match certain filename patterns.
44
  # https://github-wiki-see.page/m/koalaman/shellcheck/wiki/SC2012
45
  '\bls '
46

47
  # Ban recursive asdf calls as they are inefficient and may introduce bugs.
48
  # If you find yourself needing to invoke an `asdf` command from within
49
  # asdf code, please source the appropriate file and invoke the
50
  # corresponding function.
51
  '\basdf '
52
)
53

54
setup() {
55
  setup_asdf_dir
56
}
57

58
teardown() {
59
  clean_asdf_dir
60
}
61

62
@test "banned commands are not found in source code" {
63
  # Assert command is not used in the lib and bin dirs
64
  # or expect an explicit comment at end of line, allowing it.
65
  # Also ignore matches that are contained in comments or a string or
66
  # followed by an underscore (indicating it's a variable and not a
67
  # command).
68
  for cmd in "${banned_commands[@]}"; do
69
    run bash -c "grep -nHR --include \*.bash --include \*.sh '$cmd' asdf.* lib bin\
70
        | grep -v '#.*$cmd'\
71
        | grep -v '\".*$cmd.*\"' \
72
        | grep -v '${cmd}_'\
73
        | grep -v '# asdf_allow: $cmd'"
74

75
    # Only print output if we've found a banned command
76
    #if [ "$status" -ne 1 ]; then
77
    if [ "" != "$output" ]; then
78
      echo "banned command $cmd: $output"
79
    fi
80

81
    [ "$status" -eq 1 ]
82
    [ "" = "$output" ]
83
  done
84

85
  for cmd in "${banned_commands_regex[@]}"; do
86
    run bash -c "grep -nHRE --include \*.bash --include \*.sh '$cmd' asdf.* lib bin\
87
        | grep -v '#.*$cmd'\
88
        | grep -v '\".*$cmd.*\"' \
89
        | grep -v '${cmd}_'\
90
        | grep -v '# asdf_allow: $cmd'"
91

92
    # Only print output if we've found a banned command
93
    #if [ "$status" -ne 1 ]; then
94
    if [ "" != "$output" ]; then
95
      echo "banned command $cmd: $output"
96
    fi
97

98
    [ "$status" -eq 1 ]
99
    [ "" = "$output" ]
100
  done
101
}
102

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.