Merge branch 'hyperupcall-improve-pwd'

This commit is contained in:
Trevor Brown 2023-01-04 10:00:42 -05:00
commit 38b269fd09
10 changed files with 76 additions and 23 deletions

View File

@ -10,7 +10,7 @@ plugin_current_command() {
check_if_plugin_exists "$plugin_name"
local search_path
search_path=$(pwd)
search_path=$PWD
local version_and_path
version_and_path=$(find_versions "$plugin_name" "$search_path")
local full_version

View File

@ -42,7 +42,7 @@ display_installed_versions() {
fi
if [ -n "${versions}" ]; then
current_version=$(cut -d '|' -f 1 <<<"$(find_versions "$plugin_name" "$(pwd)")")
current_version=$(cut -d '|' -f 1 <<<"$(find_versions "$plugin_name" "$PWD")")
for version in $versions; do
flag=" "

View File

@ -39,7 +39,7 @@ get_concurrency() {
install_one_local_tool() {
local plugin_name=$1
local search_path
search_path=$(pwd)
search_path=$PWD
local plugin_versions
@ -65,7 +65,7 @@ install_local_tool_versions() {
plugins_path=$(get_plugin_path)
local search_path
search_path=$(pwd)
search_path=$PWD
local some_tools_installed
local some_plugin_not_installed

View File

@ -24,7 +24,7 @@ version_command() {
elif [ "$cmd" = "local-tree" ]; then
file=$(find_tool_versions)
else # cmd = local
file="$(pwd)/$file_name"
file="$PWD/$file_name"
fi
if [ -L "$file" ]; then

View File

@ -25,7 +25,7 @@ asdf_dir() {
export ASDF_DIR
ASDF_DIR=$(
cd "$(dirname "$(dirname "$current_script_path")")" || exit
pwd
printf '%s\n' "$PWD"
)
fi
@ -347,7 +347,7 @@ parse_legacy_version_file() {
get_preset_version_for() {
local plugin_name=$1
local search_path
search_path=$(pwd)
search_path=$PWD
local version_and_path
version_and_path=$(find_versions "$plugin_name" "$search_path")
local version
@ -451,7 +451,7 @@ find_tool_versions() {
find_file_upwards() {
local name="$1"
local search_path
search_path=$(pwd)
search_path=$PWD
while [ "$search_path" != "/" ]; do
if [ -f "$search_path/$name" ]; then
printf "%s\n" "${search_path}/$name"
@ -706,7 +706,7 @@ select_version() {
# These are separated by a space. e.g. python 3.7.2 2.7.15
# For each plugin/version pair, we check if it is present in the shim
local search_path
search_path=$(pwd)
search_path=$PWD
local shim_versions
IFS=$'\n' read -rd '' -a shim_versions <<<"$(get_shim_versions "$shim_name")"

View File

@ -30,20 +30,34 @@ class c:
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
def utilGetStrs(line, m):
return (
line[0:m.start('match')],
line[m.start('match'):m.end('match')],
line[m.end('match'):]
)
# Before: printf '%s\\n' '^w^'
# After: printf '%s\n' '^w^'
def noDoubleBackslashFixer(line: str, rule: Dict[str, str], m) -> str:
prestr = line[0:m.start('match')]
midstr = line[m.start('match'):m.end('match')]
poststr = line[m.end('match'):]
prestr, midstr, poststr = utilGetStrs(line, m)
fixed_line = f'{prestr}{midstr[1:]}{poststr}'
return fixed_line
return f'{prestr}{midstr[1:]}{poststr}'
# Before: $(pwd)
# After: $PWD
def noPwdCapture(line: str, rule: Dict[str, str], m) -> str:
prestr, midstr, poststr = utilGetStrs(line, m)
return f'{prestr}$PWD{poststr}'
def lintfile(filepath: Path, rules: List[Dict[str, str]], options: Dict[str, Any]):
content_arr = filepath.read_text().split('\n')
for line_i, line in enumerate(content_arr):
if 'checkstyle-ignore' in line:
continue
for rule in rules:
m = re.search(rule['regex'], line)
if m is not None and m.group('match') is not None:
@ -58,7 +72,7 @@ def lintfile(filepath: Path, rules: List[Dict[str, str]], options: Dict[str, Any
print()
if options['fix']:
content_arr[line_i] = rule['fixer_fn'](line, rule, m)
content_arr[line_i] = rule['fixerFn'](line, rule, m)
rule['found'] += 1
@ -71,7 +85,28 @@ def main():
'name': 'no-double-backslash',
'regex': '".*?(?P<match>\\\\\\\\[abeEfnrtv\'"?xuUc]).*?(?<!\\\\)"',
'reason': 'Backslashes are only required if followed by a $, `, ", \\, or <newline>',
'fixer_fn': noDoubleBackslashFixer,
'fixerFn': noDoubleBackslashFixer,
'testPositiveMatches': [
'printf "%s\\\\n" "Hai"',
'echo -n "Hello\\\\n"'
],
'testNegativeMatches': [
'printf "%s\\n" "Hai"',
'echo -n "Hello\\n"'
],
'found': 0
},
{
'name': 'no-pwd-capture',
'regex': '(?P<match>\\$\\(pwd\\))',
'reason': '$PWD is essentially equivalent to $(pwd) without the overhead of a subshell',
'fixerFn': noPwdCapture,
'testPositiveMatches': [
'$(pwd)'
],
'testNegativeMatches': [
'$PWD'
],
'found': 0
},
]
@ -79,8 +114,26 @@ def main():
parser = argparse.ArgumentParser()
parser.add_argument('files', metavar='FILES', nargs='*')
parser.add_argument('--fix', action='store_true')
parser.add_argument('--internal-test-regex', action='store_true')
args = parser.parse_args()
if args.internal_test_regex:
for rule in rules:
for positiveMatch in rule['testPositiveMatches']:
m = re.search(rule['regex'], positiveMatch)
if m is None or m.group('match') is None:
print(f'{c.MAGENTA}{rule["name"]}{c.RESET}: Failed {c.CYAN}positive{c.RESET} test:')
print(f'=> {positiveMatch}')
print()
for negativeMatch in rule['testNegativeMatches']:
m = re.search(rule['regex'], negativeMatch)
if m is not None and m.group('match') is not None:
print(f'{c.MAGENTA}{rule["name"]}{c.RESET}: Failed {c.YELLOW}negative{c.RESET} test:')
print(f'=> {negativeMatch}')
print()
return
options = {
'fix': args.fix
}

View File

@ -101,7 +101,7 @@ cleaned_path() {
@test "function calls asdf command" {
result=$(elvish -norc -c "
set-env ASDF_DIR $(pwd)
set-env ASDF_DIR $(pwd) # checkstyle-ignore
set paths = [$(cleaned_path)]
use asdftest _asdf; var asdf~ = \$_asdf:asdf~
asdf info

View File

@ -71,7 +71,7 @@ cleaned_path() {
@test "function calls asdf command" {
result=$(fish -c "
set -e asdf
set -x ASDF_DIR $(pwd)
set -x ASDF_DIR $(pwd) # checkstyle-ignore
set PATH $(cleaned_path)
. asdf.fish

View File

@ -89,7 +89,7 @@ cleaned_path() {
@test "function calls asdf command" {
result=$(
unset -f asdf
ASDF_DIR=$(pwd)
ASDF_DIR=$PWD
PATH=$(cleaned_path)
source_asdf_sh

View File

@ -351,11 +351,11 @@ teardown() {
@test "resolve_symlink converts the symlink path to the real file path" {
touch foo
ln -s $(pwd)/foo bar
ln -s $PWD/foo bar
run resolve_symlink bar
[ "$status" -eq 0 ]
[ "$output" = $(pwd)/foo ]
[ "$output" = $PWD/foo ]
rm -f foo bar
}
@ -365,7 +365,7 @@ teardown() {
run resolve_symlink baz/bar
[ "$status" -eq 0 ]
[ "$output" = $(pwd)/baz/../foo ]
[ "$output" = $PWD/baz/../foo ]
rm -f foo bar
}
@ -375,7 +375,7 @@ teardown() {
run resolve_symlink bar
[ "$status" -eq 0 ]
[ "$output" = $(pwd)/foo ]
[ "$output" = $PWD/foo ]
rm -f foo bar
}