From 9eea50222e7ef53df0fd12d578ed5cf681f52307 Mon Sep 17 00:00:00 2001 From: kennyballou Date: Tue, 2 Apr 2013 17:51:41 -0600 Subject: Move `which` method from test to task Add `in_path` method. Update test to use new methods --- xnt/tasks.py | 22 ++++++++++++++++++++++ xnt/tests/compilercollectiontests.py | 27 ++++----------------------- xnt/tests/taskmisctests.py | 18 ++++++++++++++++++ 3 files changed, 44 insertions(+), 23 deletions(-) (limited to 'xnt') diff --git a/xnt/tasks.py b/xnt/tasks.py index ab93f97..c11a13b 100644 --- a/xnt/tasks.py +++ b/xnt/tasks.py @@ -162,3 +162,25 @@ def setup(commands, directory=""): error_code = call(cmd) os.chdir(cwd) return error_code + +def which(program): + """Similar to Linux/Unix `which`: return (first) path of executable""" + def is_exe(fpath): + """Determine if argument exists and is executable""" + return os.path.isfile(fpath) and os.access(fpath, os.X_OK) + + fpath = os.path.split(program) + if fpath[0]: + if is_exe(program): + return program + else: + for path in os.environ["PATH"].split(os.pathsep): + path = path.strip('"') + exe_file = os.path.join(path, program) + if is_exe(exe_file): + return exe_file + return None + +def in_path(program): + """Return boolean result if program is in PATH environment variable""" + return which(program) diff --git a/xnt/tests/compilercollectiontests.py b/xnt/tests/compilercollectiontests.py index 76891a9..be72c81 100644 --- a/xnt/tests/compilercollectiontests.py +++ b/xnt/tests/compilercollectiontests.py @@ -19,31 +19,12 @@ import os import shutil +import xnt import xnt.build.cc as cc import unittest -#http://stackoverflow.com/questions/377017/test-if-executable-exists-in-python -def which(program): - """Similar to Linux/Unix `which`: return path of executable""" - def is_exe(fpath): - """Determine if arguement exists and is executable""" - return os.path.isfile(fpath) and os.access(fpath, os.X_OK) - - fpath = os.path.split(program) - if fpath[0]: - if is_exe(program): - return program - else: - for path in os.environ["PATH"].split(os.pathsep): - path = path.strip('"') - exe_file = os.path.join(path, program) - if is_exe(exe_file): - return exe_file - - return None - #pylint: disable-msg=C0103 -@unittest.skipUnless(which("gcc"), "gcc is not in your path") +@unittest.skipUnless(xnt.in_path("gcc"), "gcc is not in your path") class GccTests(unittest.TestCase): """Test GCC""" def setUp(self): @@ -75,7 +56,7 @@ class GccTests(unittest.TestCase): cc.gcc_o("temp/hello.c", "temp/hello") self.assertTrue(os.path.isfile("temp/hello")) -@unittest.skipUnless(which("g++"), "g++ is not in your path") +@unittest.skipUnless(xnt.in_path("g++"), "g++ is not in your path") class GppTests(unittest.TestCase): """Test G++ (C++ GCC)""" def setUp(self): @@ -107,7 +88,7 @@ class GppTests(unittest.TestCase): cc.gpp_o("temp/hello.cpp", "temp/hello") self.assertTrue(os.path.isfile("temp/hello")) -@unittest.skipUnless(which("javac"), "javac is not in your path") +@unittest.skipUnless(xnt.in_path("javac"), "javac is not in your path") class JavacTests(unittest.TestCase): """Test Javac""" def setUp(self): diff --git a/xnt/tests/taskmisctests.py b/xnt/tests/taskmisctests.py index f9d9adf..bcabf3d 100644 --- a/xnt/tests/taskmisctests.py +++ b/xnt/tests/taskmisctests.py @@ -58,5 +58,23 @@ class TaskMiscTests(unittest.TestCase): with open("temp/testerr", "r") as std_err: self.assertEqual("hello", std_err.read()) + def test_which_finds_python(self): + """Test which can find python""" + path = xnt.tasks.which("python") + self.assertIsNotNone(path) + + def test_which_dne(self): + """Test which cannot find not existent program""" + path = xnt.tasks.which("arst") + self.assertIsNone(path) + + def test_python_in_path(self): + """Test in_path task""" + self.assertTrue(xnt.tasks.in_path("python")) + + def test_arst_not_in_path(self): + """Test not in_path""" + self.assertFalse(xnt.tasks.in_path("arst")) + if __name__ == "__main__": unittest.main() -- cgit v1.2.1