From 64a37bb9b758630c0f2c649d82e10e849c095d48 Mon Sep 17 00:00:00 2001 From: Hyunchel Kim Date: Sun, 5 Jun 2016 10:32:00 -0700 Subject: [PATCH] Test side effect (#1889) Function `prepare_exec_for_file` has a side effect where a path is added to `sys.path` list. This commit enhances an exisiting test case for `prepare_exec_for_file` by testing the side effect of the function and adding necessary comments. --- tests/test_cli.py | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/tests/test_cli.py b/tests/test_cli.py index 1e8feb02..c4be45e6 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -12,6 +12,8 @@ # Copyright (C) 2015 CERN. # from __future__ import absolute_import, print_function +import os +import sys import click import pytest @@ -50,10 +52,23 @@ def test_find_best_app(test_apps): def test_prepare_exec_for_file(test_apps): - assert prepare_exec_for_file('test.py') == 'test' - assert prepare_exec_for_file('/usr/share/__init__.py') == 'share' + """Expect the correct path to be set and the correct module name to be returned. + + :func:`prepare_exec_for_file` has a side effect, where + the parent directory of given file is added to `sys.path`. + """ + realpath = os.path.realpath('/tmp/share/test.py') + dirname = os.path.dirname(realpath) + assert prepare_exec_for_file('/tmp/share/test.py') == 'test' + assert dirname in sys.path + + realpath = os.path.realpath('/tmp/share/__init__.py') + dirname = os.path.dirname(os.path.dirname(realpath)) + assert prepare_exec_for_file('/tmp/share/__init__.py') == 'share' + assert dirname in sys.path + with pytest.raises(NoAppException): - prepare_exec_for_file('test.txt') + prepare_exec_for_file('/tmp/share/test.txt') def test_locate_app(test_apps):