Skip to content

Commit

Permalink
scripts in a subdir are working fine now
Browse files Browse the repository at this point in the history
  • Loading branch information
csaez committed Sep 4, 2014
1 parent 8e0cef9 commit 567b8a3
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
12 changes: 7 additions & 5 deletions quicklauncher/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,18 @@ def list_commands():


def run_script(script_name):
# add repo to pythonpath
if get_repo() not in sys.path:
sys.path.append(get_repo())

# validate
script_path = get_scripts().get(script_name)
if not script_path:
return False
module_name = script_name.replace(".py", "")
# add to pythonpath and execute
if os.path.dirname(script_path) not in sys.path:
sys.path.append(os.path.dirname(script_path))
module_name = os.path.split(script_path)[-1].replace(".py", "")
__import__(module_name)
# cleanup
del sys.modules[module_name]
sys.path = sys.path[:-1]
return True


Expand Down
24 changes: 20 additions & 4 deletions quicklauncher/tests/api_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,17 +72,33 @@ def test_run_cmd():


def setup_script():
code = 'print("hello world!")'
setup_repo()
with open(os.path.join(ql.api.get_repo(), "testsuite.py"), "w") as f:
code = 'print("im in the root dir")'
f.write(code)
subdir = os.path.join(ql.api.get_repo(), "test")
if not os.path.exists(subdir):
os.mkdir(subdir)
with open(os.path.join(subdir, "testsuite.py"), "w") as f:
code = 'print("im in a subdir")'
f.write(code)


def teardown_script():
path = os.path.join(ql.api.get_repo(), "testsuite.py")
if os.path.exists(path):
os.remove(path)
for x in ("testsuite.py", "testsuite.pyc"):
path = os.path.join(ql.api.get_repo(), x)
if os.path.exists(path):
os.remove(path)
subdir = os.path.join(ql.api.get_repo(), "test")
for x in ("testsuite.py", "testsuite.pyc"):
path = os.path.join(subdir, x)
if os.path.exists(path):
os.remove(path)
os.rmdir(subdir)
teardown_repo()


@with_setup(setup_script, teardown_script)
def test_run_script():
ql.api.run_script("testsuite.py")
ql.api.run_script("test/testsuite.py")

0 comments on commit 567b8a3

Please sign in to comment.