Skip to content

Commit

Permalink
Merge pull request #5864 from ElectreAAS/exist-before-source
Browse files Browse the repository at this point in the history
Shell integration: Test if file exists before sourcing in fish + powershell
  • Loading branch information
kit-ty-kate authored Mar 4, 2024
2 parents 1eeaa74 + 87049a6 commit a4a651a
Show file tree
Hide file tree
Showing 5 changed files with 290 additions and 2 deletions.
2 changes: 2 additions & 0 deletions master_changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ users)
* Add `sys-pkg-manager-cmd` as an accepted field in opamrc files [#5847 @rjbou - fix #5844]
* Fix `git-location` handling in init config file [#5848 @rjbou - fix #5845]
* Fix MSYS2 support [#5843 @rjbou - fix #5683]
* Test if file exists before sourcing in fish + powershell [#5864 @ElectreAAS]

## Config report

Expand Down Expand Up @@ -118,6 +119,7 @@ users)

## Reftests
### Tests
* Add init scripts tests [#5864 @rjbou]

### Engine

Expand Down
4 changes: 2 additions & 2 deletions src/state/opamEnv.ml
Original file line number Diff line number Diff line change
Expand Up @@ -887,7 +887,7 @@ let source root shell f =
fname fname
| SH_fish ->
let fname = unix_transform ~using_backslashes:true () in
Printf.sprintf "source '%s' > /dev/null 2> /dev/null; or true\n" fname
Printf.sprintf "test -r '%s' && source '%s' > /dev/null 2> /dev/null; or true\n" fname fname
| SH_sh | SH_bash ->
let fname = unix_transform () in
Printf.sprintf "test -r '%s' && . '%s' > /dev/null 2> /dev/null || true\n"
Expand All @@ -899,7 +899,7 @@ let source root shell f =
| SH_cmd ->
Printf.sprintf "if exist \"%s\" call \"%s\" >NUL 2>NUL\n" fname fname
| SH_pwsh _ ->
Printf.sprintf ". \"%s\" *> $null\n" fname
Printf.sprintf "if Test-Path \"%s\" { . \"%s\" *> $null }\n" fname fname

let if_interactive_script shell t e =
let ielse else_opt = match else_opt with
Expand Down
42 changes: 42 additions & 0 deletions tests/reftests/dune.inc
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,48 @@
%{targets}
(run ./run.exe %{exe:../../src/client/opamMain.exe.exe} %{dep:extrasource.test} %{read-lines:testing-env}))))

(rule
(alias reftest-init-scripts.unix)
(enabled_if (= %{os_type} "Unix"))
(action
(diff init-scripts.unix.test init-scripts.unix.out)))

(alias
(name reftest)
(enabled_if (= %{os_type} "Unix"))
(deps (alias reftest-init-scripts.unix)))

(rule
(targets init-scripts.unix.out)
(deps root-N0REP0)
(enabled_if (= %{os_type} "Unix"))
(package opam)
(action
(with-stdout-to
%{targets}
(run ./run.exe %{exe:../../src/client/opamMain.exe.exe} %{dep:init-scripts.unix.test} %{read-lines:testing-env}))))

(rule
(alias reftest-init-scripts.win32)
(enabled_if (= %{os_type} "Win32"))
(action
(diff init-scripts.win32.test init-scripts.win32.out)))

(alias
(name reftest)
(enabled_if (= %{os_type} "Win32"))
(deps (alias reftest-init-scripts.win32)))

(rule
(targets init-scripts.win32.out)
(deps root-N0REP0)
(enabled_if (= %{os_type} "Win32"))
(package opam)
(action
(with-stdout-to
%{targets}
(run ./run.exe %{exe:../../src/client/opamMain.exe.exe} %{dep:init-scripts.win32.test} %{read-lines:testing-env}))))

(rule
(alias reftest-init)
(action
Expand Down
124 changes: 124 additions & 0 deletions tests/reftests/init-scripts.unix.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
N0REP0
### rm -rf $OPAMROOT
### opam init -na --bypass-checks --bare REPO --root root
No configuration file found, using built-in defaults.

<><> Fetching repository information ><><><><><><><><><><><><><><><><><><><><><>
[default] Initialised
### # we need the switch for variables file
### opam switch create fake --empty --root root
### ls root/opam-init | unordered
complete.sh
complete.zsh
env_hook.csh
env_hook.fish
env_hook.sh
env_hook.zsh
hooks
init.cmd
init.csh
init.fish
init.ps1
init.sh
init.zsh
variables.cmd
variables.csh
variables.fish
variables.ps1
variables.sh
### : Init scripts :
### cat root/opam-init/init.sh
if [ -t 0 ]; then
test -r '${BASEDIR}/root/opam-init/complete.sh' && . '${BASEDIR}/root/opam-init/complete.sh' > /dev/null 2> /dev/null || true

test -r '${BASEDIR}/root/opam-init/env_hook.sh' && . '${BASEDIR}/root/opam-init/env_hook.sh' > /dev/null 2> /dev/null || true
fi

test -r '${BASEDIR}/root/opam-init/variables.sh' && . '${BASEDIR}/root/opam-init/variables.sh' > /dev/null 2> /dev/null || true
### cat root/opam-init/init.zsh
if [[ -o interactive ]]; then
[[ ! -r '${BASEDIR}/root/opam-init/complete.zsh' ]] || source '${BASEDIR}/root/opam-init/complete.zsh' > /dev/null 2> /dev/null

[[ ! -r '${BASEDIR}/root/opam-init/env_hook.zsh' ]] || source '${BASEDIR}/root/opam-init/env_hook.zsh' > /dev/null 2> /dev/null
fi

[[ ! -r '${BASEDIR}/root/opam-init/variables.sh' ]] || source '${BASEDIR}/root/opam-init/variables.sh' > /dev/null 2> /dev/null
### cat root/opam-init/init.fish
if isatty
test -r '${BASEDIR}/root/opam-init/env_hook.fish' && source '${BASEDIR}/root/opam-init/env_hook.fish' > /dev/null 2> /dev/null; or true
end

test -r '${BASEDIR}/root/opam-init/variables.fish' && source '${BASEDIR}/root/opam-init/variables.fish' > /dev/null 2> /dev/null; or true
### cat root/opam-init/init.csh
if ( $?prompt ) then
if ( -f '${BASEDIR}/root/opam-init/env_hook.csh' ) source '${BASEDIR}/root/opam-init/env_hook.csh' >& /dev/null
endif

if ( -f '${BASEDIR}/root/opam-init/variables.csh' ) source '${BASEDIR}/root/opam-init/variables.csh' >& /dev/null
### cat root/opam-init/init.cmd
if exist "${BASEDIR}/root/opam-init/variables.cmd" call "${BASEDIR}/root/opam-init/variables.cmd" >NUL 2>NUL
### cat root/opam-init/init.ps1
if Test-Path "${BASEDIR}/root/opam-init/variables.ps1" { . "${BASEDIR}/root/opam-init/variables.ps1" *> $null }
### : Variables scripts :
### cat root/opam-init/variables.sh | grep -v man | grep -v MANPATH
# Prefix of the current opam switch
OPAM_SWITCH_PREFIX='${BASEDIR}/root/fake'; export OPAM_SWITCH_PREFIX;
# Binary dir for opam switch fake
PATH='${BASEDIR}/root/fake/bin':"$PATH"; export PATH;
### test -f root/opam-init/variables.zsh
# Return code 1 #
### cat root/opam-init/variables.fish | grep -v man | grep -v MANPATH
# Prefix of the current opam switch
set -gx OPAM_SWITCH_PREFIX '${BASEDIR}/root/fake';
# Binary dir for opam switch fake
set -gx PATH '${BASEDIR}/root/fake/bin' $PATH;
### cat root/opam-init/variables.csh | grep -v man | grep -v MANPATH
# Prefix of the current opam switch
if ( ! ${?OPAM_SWITCH_PREFIX} ) setenv OPAM_SWITCH_PREFIX ""
setenv OPAM_SWITCH_PREFIX '${BASEDIR}/root/fake'
# Binary dir for opam switch fake
if ( ! ${?PATH} ) setenv PATH ""
setenv PATH '${BASEDIR}/root/fake/bin':"$PATH"
### cat root/opam-init/variables.cmd | grep -v man | grep -v MANPATH
:: Prefix of the current opam switch
set "OPAM_SWITCH_PREFIX=${BASEDIR}/root/fake"
:: Binary dir for opam switch fake
set "PATH=${BASEDIR}/root/fake/bin:%PATH%"
### cat root/opam-init/variables.ps1 | grep -v man | grep -v MANPATH
# Prefix of the current opam switch
$env:OPAM_SWITCH_PREFIX='${BASEDIR}/root/fake'
# Binary dir for opam switch fake
$env:PATH='${BASEDIR}/root/fake/bin:' + "$env:PATH"
### : Env hook scripts :
### cat root/opam-init/env_hook.sh
OPAMNOENVNOTICE=true; export OPAMNOENVNOTICE;
_opam_env_hook() {
local previous_exit_status=$?;
eval $(opam env --shell=bash --readonly 2> /dev/null <&- );
return $previous_exit_status;
};
if ! [[ "$PROMPT_COMMAND" =~ _opam_env_hook ]]; then
PROMPT_COMMAND="_opam_env_hook;$PROMPT_COMMAND";
fi
### cat root/opam-init/env_hook.zsh
OPAMNOENVNOTICE=true; export OPAMNOENVNOTICE;
_opam_env_hook() {
eval $(opam env --shell=zsh --readonly 2> /dev/null <&-);
}
typeset -ag precmd_functions;
if [[ -z ${precmd_functions[(r)_opam_env_hook]} ]]; then
precmd_functions+=_opam_env_hook;
fi
### cat root/opam-init/env_hook.fish
set -gx OPAMNOENVNOTICE true;
function __opam_env_export_eval --on-event fish_prompt;
eval (opam env --shell=fish --readonly 2> /dev/null);
end
### cat root/opam-init/env_hook.csh
if ( ! ${?OPAMNOENVNOTICE} ) setenv OPAMNOENVNOTICE ""
setenv OPAMNOENVNOTICE true
alias precmd 'eval `opam env --shell=csh --readonly`'
### test -f root/opam-init/env_hook.cmd
# Return code 1 #
### test -f root/opam-init/env_hook.ps1
# Return code 1 #
120 changes: 120 additions & 0 deletions tests/reftests/init-scripts.win32.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
N0REP0
### rm -rf $OPAMROOT
### opam init -na --bypass-checks --bare REPO --root root | grep -v Cygwin
No configuration file found, using built-in defaults.

<><> Fetching repository information ><><><><><><><><><><><><><><><><><><><><><>
[default] Initialised
### # we need the switch for variables file
### opam switch create fake --empty --root root
### ls root/opam-init | unordered
complete.sh
complete.zsh
env_hook.csh
env_hook.fish
env_hook.sh
env_hook.zsh
init.cmd
init.csh
init.fish
init.ps1
init.sh
init.zsh
variables.cmd
variables.csh
variables.fish
variables.ps1
variables.sh
### : Init scripts :
### cat root/opam-init/init.sh
if [ -t 0 ]; then
test -r '${BASEDIR}/root/opam-init/complete.sh' && . '${BASEDIR}/root/opam-init/complete.sh' > /dev/null 2> /dev/null || true

test -r '${BASEDIR}/root/opam-init/env_hook.sh' && . '${BASEDIR}/root/opam-init/env_hook.sh' > /dev/null 2> /dev/null || true
fi

test -r '${BASEDIR}/root/opam-init/variables.sh' && . '${BASEDIR}/root/opam-init/variables.sh' > /dev/null 2> /dev/null || true
### cat root/opam-init/init.zsh
if [[ -o interactive ]]; then
[[ ! -r '${BASEDIR}/root/opam-init/complete.zsh' ]] || source '${BASEDIR}/root/opam-init/complete.zsh' > /dev/null 2> /dev/null

[[ ! -r '${BASEDIR}/root/opam-init/env_hook.zsh' ]] || source '${BASEDIR}/root/opam-init/env_hook.zsh' > /dev/null 2> /dev/null
fi

[[ ! -r '${BASEDIR}/root/opam-init/variables.sh' ]] || source '${BASEDIR}/root/opam-init/variables.sh' > /dev/null 2> /dev/null
### cat root/opam-init/init.fish
if isatty
test -r '${BASEDIR}/root/opam-init/env_hook.fish' && source '${BASEDIR}/root/opam-init/env_hook.fish' > /dev/null 2> /dev/null; or true
end

test -r '${BASEDIR}/root/opam-init/variables.fish' && source '${BASEDIR}/root/opam-init/variables.fish' > /dev/null 2> /dev/null; or true
### cat root/opam-init/init.csh
if ( $?prompt ) then
if ( -f '${BASEDIR}/root/opam-init/env_hook.csh' ) source '${BASEDIR}/root/opam-init/env_hook.csh' >& /dev/null
endif

if ( -f '${BASEDIR}/root/opam-init/variables.csh' ) source '${BASEDIR}/root/opam-init/variables.csh' >& /dev/null
### cat root/opam-init/init.cmd
if exist "${BASEDIR}/root/opam-init/variables.cmd" call "${BASEDIR}/root/opam-init/variables.cmd" >NUL 2>NUL
### cat root/opam-init/init.ps1
if Test-Path "${BASEDIR}/root/opam-init/variables.ps1" { . "${BASEDIR}/root/opam-init/variables.ps1" *> $null }
### : Variables scripts :
### cat root/opam-init/variables.sh | grep -v man | grep -v MANPATH
# Prefix of the current opam switch
OPAM_SWITCH_PREFIX='${BASEDIR}/root/fake'; export OPAM_SWITCH_PREFIX;
# Binary dir for opam switch fake
PATH='${BASEDIR}/root/fake/bin':"$PATH"; export PATH;
### test -f root/opam-init/variables.zsh
# Return code 1 #
### # ERROR, broken, see opam#5854 testing only its existence
### test -f root/opam-init/variables.fish
### cat root/opam-init/variables.csh | grep -v man | grep -v MANPATH
# Prefix of the current opam switch
if ( ! ${?OPAM_SWITCH_PREFIX} ) setenv OPAM_SWITCH_PREFIX ""
setenv OPAM_SWITCH_PREFIX '${BASEDIR}/root/fake'
# Binary dir for opam switch fake
if ( ! ${?PATH} ) setenv PATH ""
setenv PATH '${BASEDIR}/root/fake/bin':"$PATH"
### cat root/opam-init/variables.cmd | grep -v man | grep -v MANPATH
:: Prefix of the current opam switch
set "OPAM_SWITCH_PREFIX=${BASEDIR}/root/fake"
:: Binary dir for opam switch fake
set "PATH=${BASEDIR}/root/fake/bin;%PATH%"
### cat root/opam-init/variables.ps1 | grep -v man | grep -v MANPATH
# Prefix of the current opam switch
$env:OPAM_SWITCH_PREFIX='${BASEDIR}/root/fake'
# Binary dir for opam switch fake
$env:PATH='${BASEDIR}/root/fake/bin;' + "$env:PATH"
### : Env hook scripts :
### cat root/opam-init/env_hook.sh
OPAMNOENVNOTICE=true; export OPAMNOENVNOTICE;
_opam_env_hook() {
local previous_exit_status=$?;
eval $(opam env --shell=bash --readonly 2> /dev/null <&- );
return $previous_exit_status;
};
if ! [[ "$PROMPT_COMMAND" =~ _opam_env_hook ]]; then
PROMPT_COMMAND="_opam_env_hook;$PROMPT_COMMAND";
fi
### cat root/opam-init/env_hook.zsh
OPAMNOENVNOTICE=true; export OPAMNOENVNOTICE;
_opam_env_hook() {
eval $(opam env --shell=zsh --readonly 2> /dev/null <&-);
}
typeset -ag precmd_functions;
if [[ -z ${precmd_functions[(r)_opam_env_hook]} ]]; then
precmd_functions+=_opam_env_hook;
fi
### cat root/opam-init/env_hook.fish
set -gx OPAMNOENVNOTICE true;
function __opam_env_export_eval --on-event fish_prompt;
eval (opam env --shell=fish --readonly 2> /dev/null);
end
### cat root/opam-init/env_hook.csh
if ( ! ${?OPAMNOENVNOTICE} ) setenv OPAMNOENVNOTICE ""
setenv OPAMNOENVNOTICE true
alias precmd 'eval `opam env --shell=csh --readonly`'
### test -f root/opam-init/env_hook.cmd
# Return code 1 #
### test -f root/opam-init/env_hook.ps1
# Return code 1 #

0 comments on commit a4a651a

Please sign in to comment.