From eabf9091fb5daedb7f2eb52afb51e1e8cdfb7c5f Mon Sep 17 00:00:00 2001 From: Tim Byrne Date: Sun, 9 Apr 2017 07:56:27 -0500 Subject: [PATCH] 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. --- test/006_unit_set_operating_system.bats | 76 +++++++++++++++++++++++++ test/113_accept_jinja_alt.bats | 2 +- yadm | 43 +++++++++----- 3 files changed, 106 insertions(+), 15 deletions(-) create mode 100644 test/006_unit_set_operating_system.bats diff --git a/test/006_unit_set_operating_system.bats b/test/006_unit_set_operating_system.bats new file mode 100644 index 0000000..6bbe2c6 --- /dev/null +++ b/test/006_unit_set_operating_system.bats @@ -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" ] +} diff --git a/test/113_accept_jinja_alt.bats b/test/113_accept_jinja_alt.bats index 0847ea9..6ed6c63 100644 --- a/test/113_accept_jinja_alt.bats +++ b/test/113_accept_jinja_alt.bats @@ -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 diff --git a/yadm b/yadm index 19bd4eb..366845e 100755 --- a/yadm +++ b/yadm @@ -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