Repository: theacodes/nox
Pull request #171 (2019-02-21 17:26:27)

Expand environment variables when loading the noxfile from provided path
Our CI setup uploads the repo to the system's temporary directory, which on Linux is known(or commonly enough `/tmp`) but on windows, `%TEMP%` is used:
```
❯ bundle exec kitchen verify py3-windows-2012r2
-----> Starting Kitchen (v1.23.5)
-----> Verifying <py3-windows-2012r2>...
       [Nox] Verify on instance py3-windows-2012r2 with state={:username=>"administrator", :auto_security_group_id=>"sg-0cf1cc52c2d0e5d24", :server_id=>"i-0f2b90b1cfedb970e", :hostname=>"10.27.2.213", :password=>"uDU9*;ZH(j", :last_action=
>"verify", :last_error=>nil}
       Running Command: nox -f %TEMP%\kitchen/testing/noxfile.py -e "runtests-3(coverage=True)" -- --output-columns=120 --sysinfo --xml=%TEMP%\kitchen/testing/artifacts/xml-unittests-output --names-file=%TEMP%\kitchen\testing\tests\whiteli
st.txt --transport=zeromq -v --run-destructive --ssh-tests    -n tests\unit 2>&1
       nox : Noxfile C:\Users\Administrator\Documents\%TEMP%\kitchen\testing\noxfile.py not found.
       At line:1 char:1
       + nox -f %TEMP%\kitchen/testing/noxfile.py -e "runtests-3(coverage=True)" -- --out ...
       + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
           + CategoryInfo          : NotSpecified: (Noxfile C:\User...e.py not found.:String) [], RemoteException
           + FullyQualifiedErrorId : NativeCommandError
        
       Verify command failed :: WinRM exited (2) for command: [nox -f %TEMP%\kitchen/testing/noxfile.py -e "runtests-3(coverage=True)" -- --output-columns=120 --sysinfo --xml=%TEMP%\kitchen/testing/artifacts/xml-unittests-output --names-fi
le=%TEMP%\kitchen\testing\tests\whitelist.txt --transport=zeromq -v --run-destructive --ssh-tests    -n tests\unit 2>&1]                                                                                                                      
       Copying %TEMP%\kitchen/testing/artifacts to /home/vampas/projects/SaltStack/salt/features/nox-2017.7/
       Copying c:/salt/var/log/salt/minion to artifacts/logs/minion
>>>>>> Failed to copy c:/salt/var/log/salt/minion to artifacts/logs/minion :: ${e}
       Finished verifying <py3-windows-2012r2> (0m14.00s).
-----> Kitchen is finished. (0m16.95s)
```
This PR processes the passed `noxfile` through `os.path.expandvars` to accommodate the above scenario.

theacodes: Thanks for writing this @s0undt3ch and thanks for reviewing, @dhermes. :)

Patch:
diff --git a/nox/tasks.py b/nox/tasks.py
index 1533f01..f392f65 100644
--- a/nox/tasks.py
+++ b/nox/tasks.py
@@ -41,7 +41,10 @@ def load_nox_module(global_config):
         # Save the absolute path to the Noxfile.
         # This will inoculate it if nox changes paths because of an implicit
         # or explicit chdir (like the one below).
-        global_config.noxfile = os.path.realpath(global_config.noxfile)
+        global_config.noxfile = os.path.realpath(
+            # Be sure to expand variables
+            os.path.expandvars(global_config.noxfile)
+        )
 
         # Move to the path where the Noxfile is.
         # This will ensure that the Noxfile's path is on sys.path, and that


Test patch:
diff --git a/tests/test_sessions.py b/tests/test_sessions.py
index 58dcd53..0419401 100644
--- a/tests/test_sessions.py
+++ b/tests/test_sessions.py
@@ -400,7 +400,7 @@ class TestSessionRunner:
 
         create.assert_called_once_with(runner.venv)
         assert isinstance(runner.venv, nox.virtualenv.VirtualEnv)
-        assert runner.venv.interpreter is "coolpython"
+        assert runner.venv.interpreter == "coolpython"
         assert runner.venv.reuse_existing is True
 
     def make_runner_with_mock_venv(self):
diff --git a/tests/test_tasks.py b/tests/test_tasks.py
index 75afd42..5005956 100644
--- a/tests/test_tasks.py
+++ b/tests/test_tasks.py
@@ -17,6 +17,7 @@ import copy
 import io
 import json
 import os
+import platform
 from unittest import mock
 
 import pytest
@@ -43,6 +44,20 @@ def test_load_nox_module():
     assert noxfile_module.SIGIL == "123"
 
 
+def test_load_nox_module_expandvars():
+    # Assert that variables are expanded when looking up the path to the noxfile
+    # This is particular importand in Windows when one needs to use variables like
+    # %TEMP% to point to the noxfile.py
+    with mock.patch.dict(os.environ, {"RESOURCES_PATH": RESOURCES}):
+        if platform.system().lower().startswith("win"):
+            config = argparse.Namespace(noxfile="%RESOURCES_PATH%/noxfile.py")
+        else:
+            config = argparse.Namespace(noxfile="${RESOURCES_PATH}/noxfile.py")
+        noxfile_module = tasks.load_nox_module(config)
+    assert noxfile_module.__file__ == os.path.join(RESOURCES, "noxfile.py")
+    assert noxfile_module.SIGIL == "123"
+
+
 def test_load_nox_module_not_found():
     config = argparse.Namespace(noxfile="bogus.py")
     assert tasks.load_nox_module(config) == 2
diff --git a/tests/test_virtualenv.py b/tests/test_virtualenv.py
index 525b0cf..4527cb8 100644
--- a/tests/test_virtualenv.py
+++ b/tests/test_virtualenv.py
@@ -54,7 +54,7 @@ def test_constructor_defaults(make_one):
 def test_constructor_explicit(make_one):
     venv, _ = make_one(interpreter="python3.5", reuse_existing=True)
     assert venv.location
-    assert venv.interpreter is "python3.5"
+    assert venv.interpreter == "python3.5"
     assert venv.reuse_existing is True