Support WSL detection (#61)

`uname -s` was already being executed every run for *cygwin* detection.
I've consolidated all of the OS detection into a single function. This
also fixed the problem of running `uname -s` twice for the `alt`
command.
This commit is contained in:
Tim Byrne 2017-04-09 07:56:27 -05:00
parent df4652d6de
commit eabf9091fb
No known key found for this signature in database
GPG Key ID: 14DB4FC2465A4B12
3 changed files with 106 additions and 15 deletions

View File

@ -0,0 +1,76 @@
load common
load_fixtures
@test "Default OS" {
echo "
By default, the value of OPERATING_SYSTEM should be reported by uname -s
"
# shellcheck source=/dev/null
YADM_TEST=1 source "$T_YADM"
status=0
output=$( set_operating_system; echo "$OPERATING_SYSTEM" ) || {
status=$?
true
}
expected=$(uname -s 2>/dev/null)
echo "output=$output"
echo "expect=$expected"
[ "$status" == 0 ]
[ "$output" = "$expected" ]
}
@test "Detect no WSL" {
echo "
When /proc/version does not contain Microsoft, report uname -s
"
echo "proc version exists" > "$BATS_TMPDIR/proc_version"
# shellcheck source=/dev/null
YADM_TEST=1 source "$T_YADM"
# shellcheck disable=SC2034
PROC_VERSION="$BATS_TMPDIR/proc_version"
status=0
output=$( set_operating_system; echo "$OPERATING_SYSTEM" ) || {
status=$?
true
}
expected=$(uname -s 2>/dev/null)
echo "output=$output"
echo "expect=$expected"
[ "$status" == 0 ]
[ "$output" = "$expected" ]
}
@test "Detect WSL" {
echo "
When /proc/version contains Microsoft, report WSL
"
echo "proc version contains Microsoft in it" > "$BATS_TMPDIR/proc_version"
# shellcheck source=/dev/null
YADM_TEST=1 source "$T_YADM"
# shellcheck disable=SC2034
PROC_VERSION="$BATS_TMPDIR/proc_version"
status=0
output=$( set_operating_system; echo "$OPERATING_SYSTEM" ) || {
status=$?
true
}
expected="WSL"
echo "output=$output"
echo "expect=$expected"
[ "$status" == 0 ]
[ "$output" = "$expected" ]
}

View File

@ -94,7 +94,7 @@ function test_alt() {
# shellcheck source=/dev/null
YADM_TEST=1 source "$T_YADM"
process_global_args -Y "$T_DIR_YADM"
test_cygwin
set_operating_system
configure_paths
status=0

43
yadm
View File

@ -35,6 +35,9 @@ GIT_PROGRAM="git"
LS_PROGRAM="/bin/ls"
ENVTPL_PROGRAM="envtpl"
PROC_VERSION="/proc/version"
OPERATING_SYSTEM="Unknown"
#; flag causing path translations with cygpath
USE_CYGPATH=0
@ -125,7 +128,7 @@ function alt() {
local_system="$(config local.os)"
if [ -z "$local_system" ] ; then
local_system=$(uname -s)
local_system="$OPERATING_SYSTEM"
fi
match_system="(%|$local_system)"
@ -735,6 +738,30 @@ function configure_repo() {
}
function set_operating_system() {
#; special detection of WSL (windows subsystem for linux)
local proc_version
proc_version=$(cat "$PROC_VERSION" 2>/dev/null)
if [[ "$proc_version" =~ Microsoft ]]; then
OPERATING_SYSTEM="WSL"
else
OPERATING_SYSTEM=$(uname -s)
fi
case "$OPERATING_SYSTEM" in
CYGWIN*)
git_version=$(git --version 2>/dev/null)
if [[ "$git_version" =~ windows ]] ; then
USE_CYGPATH=1
fi
;;
*)
;;
esac
}
function debug() {
[ -n "$DEBUG" ] && echo -e "DEBUG: $*"
@ -853,18 +880,6 @@ function envtpl_available() {
#; ****** Directory tranlations ******
function test_cygwin() {
case "$(uname -s)" in
CYGWIN*)
git_version=$(git --version 2>/dev/null)
if [[ "$git_version" =~ windows ]] ; then
USE_CYGPATH=1
fi
;;
*)
;;
esac
}
function unix_path() {
#; for paths used by bash/yadm
if [ "$USE_CYGPATH" = "1" ] ; then
@ -886,7 +901,7 @@ function mixed_path() {
if [ "$YADM_TEST" != 1 ] ; then
process_global_args "$@"
test_cygwin
set_operating_system
configure_paths
main "${MAIN_ARGS[@]}"
fi