Как программно изменить "/test/unit/python/tests.py" на "test.unit.python.tests" в Bash? - PullRequest
1 голос
/ 21 мая 2019

В каталоге test много файлов python.

file="test/unit/python/tests.py"

Я хочу как-то вызвать все скрипты Python для модульного тестирования.

python -m unittest test.unit.python.tests

Как программно добавить подстроку "/test/unit/python/tests.py" в "test.unit.python.tests" в Bash?

$ echo $file    ===> /test/unit/python/tests.py
$ '${file.???}' ===> How can I get test.unit.python.tests 
                =>              or test.unit.python.tests.py

Ответы [ 3 ]

2 голосов
/ 21 мая 2019

Использовать расширение параметра:

$ file='test/unit/python/tests.py'
$ basename=${file%.py}               # basename is test/unit/python/tests
$ printf '%s\n' "${basename//\//.}"  # replaces all "/" with "."
test.unit.python.tests
1 голос
/ 21 мая 2019

Вот реализация на чистом Bash:

python_path_to_dotted() {
  if [[ "$1" == *.py ]]; then
    return 1
  fi
  IFS='/' read -a pypath_parts <<< "$1"
  pypath_dotted="$(printf '%s.' "${pypath_parts[@]}")"
  printf '%s\n' "${pypath_dotted%%.py.}"
}

Объявите эту функцию, и тогда вы можете сделать что-то вроде:

file='path/to/module.py'
python -m unittest "$(python_path_to_dotted "$file")"
0 голосов
/ 21 мая 2019

Вы можете использовать sed:

>file="test/unit/python/tests.py"
>echo $file | sed 's/\//\./g' | sed 's/\.py//g'
>test.unit.python.tests # output

Использовать с питоном (также убрал .py):

python -m unittest $(echo $file | sed 's/\//\./g' | sed 's/\.py//g')
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...