Skip to content

Commit

Permalink
Merge pull request #5743 from rjbou/switch-revert
Browse files Browse the repository at this point in the history
`OPAMSWITCH` and `OPAMROOT` reverts
  • Loading branch information
kit-ty-kate authored Jan 11, 2024
2 parents 71631ca + 7f5f055 commit 5cd7e1b
Show file tree
Hide file tree
Showing 3 changed files with 171 additions and 1 deletion.
1 change: 1 addition & 0 deletions master_changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ users)
## Reftests
### Tests
* Add some additional test to tree, for `--dev` && `--no-switch` [#5687 @rjbou]
* switch-set: add test that checks unsetting `OPAMSWITCH` when it was set by `opam env --set-switch` on an already set `OPAMSWITCH` variable in environment [#5742 rjbou]

### Engine
* Set `SHELL` to `/bin/sh` in Windows to ensure `opam env` commands are consistent [#5723 @dra27]
Expand Down
10 changes: 9 additions & 1 deletion src/client/opamConfigCommand.ml
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,15 @@ let ensure_env_aux ?(base=[]) ?(set_opamroot=false) ?(set_opamswitch=false)
not (String.equal upd.envu_var "OPAM_LAST_ENV"))
updates
in
let last_env_file = write_last_env_file gt switch updates in
let last_env_file =
write_last_env_file gt switch
(* We remove OPAMSWITCH & OPAMROOT as they are not supposed
to be reverted *)
(List.filter (fun upd ->
not ((String.equal upd.envu_var "OPAMSWITCH")
|| (String.equal upd.envu_var "OPAMROOT")))
updates)
in
let updates =
OpamStd.Option.map_default (fun target ->
(env_update_resolved "OPAM_LAST_ENV" Eq
Expand Down
161 changes: 161 additions & 0 deletions tests/reftests/switch-set.test
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,164 @@ Selecting opam switch -.
Would select opam switch bar.
### opam switch show
-
### : OPAMSWITCH & OPAMROOT specific handling on last env storage and reverts :
### opam sw create foo --empty
### <set-switch.sh>
unset `env | grep OPAM | cut -f 1 -d = | grep -v OPAMROOT | grep -v OPAMNOENVNOTICE`
case $1 in
opam)
opam sw foo
;;
env)
export OPAMSWITCH=foo
;;
esac
echo "# current switch"
opam sw show
echo "# OPAMSWITCH current value"
echo $OPAMSWITCH
echo "# opam env"
echo "$(opam env)" | grep OPAMSWITCH
echo "# opam env with switch bar"
echo "$(opam env --set-switch --sw bar)" | grep OPAMSWITCH
eval $(opam env --set-switch --sw bar)
echo "# OPAMSWITCH current value"
echo $OPAMSWITCH
echo "# opam env"
echo "$(opam env)" | grep OPAMSWITCH
echo "# OPAMSWITCH current value"
echo $OPAMSWITCH
echo "# current switch"
opam sw show
echo "# evaluate opam env"
eval $(opam env)
echo "# OPAMSWITCH current value"
echo $OPAMSWITCH
echo "# current switch"
opam sw show
### sh set-switch.sh opam
# current switch
foo
# OPAMSWITCH current value

# opam env
# opam env with switch bar
OPAMSWITCH='bar'; export OPAMSWITCH;
# OPAMSWITCH current value
bar
# opam env
# OPAMSWITCH current value
bar
# current switch
bar
# evaluate opam env
# OPAMSWITCH current value
bar
# current switch
bar
### sh set-switch.sh env
# current switch
foo
# OPAMSWITCH current value
foo
# opam env
# opam env with switch bar
OPAMSWITCH='bar'; export OPAMSWITCH;
# OPAMSWITCH current value
bar
# opam env
# OPAMSWITCH current value
bar
# current switch
bar
# evaluate opam env
# OPAMSWITCH current value
bar
# current switch
bar
### opam init --bare --bypass-checks --root ./foo ./REPO -n | grep -v Cygwin
No configuration file found, using built-in defaults.

<><> Fetching repository information ><><><><><><><><><><><><><><><><><><><><><>
[default] Initialised
### opam switch create sw-foo --root ./foo --empty
### opam init --bare --bypass-checks --root ./bar ./REPO -n | grep -v Cygwin
No configuration file found, using built-in defaults.

<><> Fetching repository information ><><><><><><><><><><><><><><><><><><><><><>
[default] Initialised
### opam switch create sw-bar --root ./bar --empty
### <set-root.sh>
unset `env | grep OPAM | cut -f 1 -d = | grep -v OPAMNOENVNOTICE`
case $1 in
opam)
ARGF="--root foo"
;;
env)
export OPAMROOT=${BASEDIR}/foo
;;
esac
echo "# current root"
opam var root $ARGF
echo "# OPAMROOT current value"
echo $OPAMROOT
echo "# opam env"
echo "$(opam env $ARGF)" | grep OPAMROOT
echo "# opam env with root bar"
echo "$(opam env --set-root --root ./bar)" | grep OPAMROOT
eval $(opam env --set-root --root ./bar)
echo "# OPAMROOT current value"
echo $OPAMROOT
echo "# opam env"
echo "$(opam env)" | grep OPAMROOT
echo "# OPAMROOT current value"
echo $OPAMROOT
echo "# current root"
opam var root
echo "evaluate opam env"
eval $(opam env)
echo "# OPAMROOT current value"
echo $OPAMROOT
echo "# current root"
opam var root
### sh set-root.sh opam
# current root
${BASEDIR}/foo
# OPAMROOT current value

# opam env
[NOTE] To make opam select ${BASEDIR}/foo as its root in the current shell, add --set-root or set OPAMROOT
# opam env with root bar
OPAMROOT='${BASEDIR}/bar'; export OPAMROOT;
# OPAMROOT current value
${BASEDIR}/bar
# opam env
# OPAMROOT current value
${BASEDIR}/bar
# current root
${BASEDIR}/bar
evaluate opam env
# OPAMROOT current value
${BASEDIR}/bar
# current root
${BASEDIR}/bar
### sh set-root.sh env
# current root
${BASEDIR}/foo
# OPAMROOT current value
${BASEDIR}/foo
# opam env
# opam env with root bar
OPAMROOT='${BASEDIR}/bar'; export OPAMROOT;
# OPAMROOT current value
${BASEDIR}/bar
# opam env
# OPAMROOT current value
${BASEDIR}/bar
# current root
${BASEDIR}/bar
evaluate opam env
# OPAMROOT current value
${BASEDIR}/bar
# current root
${BASEDIR}/bar

0 comments on commit 5cd7e1b

Please sign in to comment.