Skip to content

Commit

Permalink
Merge fef601d into afd1760
Browse files Browse the repository at this point in the history
  • Loading branch information
gilgamezh committed Feb 6, 2019
2 parents afd1760 + fef601d commit cb27d27
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 12 deletions.
26 changes: 19 additions & 7 deletions fades/main.py
Expand Up @@ -96,14 +96,33 @@ def consolidate_dependencies(needs_ipython, child_program,

def decide_child_program(args_executable, args_child_program):
"""Decide which the child program really is (if any)."""
# We get the logger here because it's not defined at module level
logger = logging.getLogger('fades')

if args_executable:
# if --exec given, check that it's just the executable name,
# not absolute or relative paths
if args_executable and os.path.sep in args_child_program:
logger.error(
"The parameter to --exec must be a file name (to be found "
"inside venv's bin directory), not a file path: %r",
args_child_program)
raise FadesError("File path given to --exec parameter")

# indicated --execute, local and not analyzable for dependencies
analyzable_child_program = None
child_program = args_child_program
elif args_child_program is not None:
# normal case, the child program is to be analyzed (being it local or remote)
if args_child_program.startswith(("http://", "https://")):
args_child_program = helpers.download_remote_script(args_child_program)
else:
if not os.access(args_child_program, os.R_OK):
logger.error("'%s' not found. If you wan to run an executable "
"file from a library installed in the virtualenv "
"check the `--exec` option in the help.",
args_child_program)
raise FadesError("child program not found.")
analyzable_child_program = args_child_program
child_program = args_child_program
else:
Expand Down Expand Up @@ -258,13 +277,6 @@ def go():
logger.warning('No virtualenv found with uuid: %s.', uuid)
return 0

# if --exec given, check that it's just the executable name, not absolute or relative paths
if args.executable and os.path.sep in args.child_program:
logger.error(
"The parameter to --exec must be a file name (to be found "
"inside venv's bin directory), not a file path: %r", args.child_program)
raise FadesError("File path given to --exec parameter")

# decided which the child program really is
analyzable_child_program, child_program = decide_child_program(
args.executable, args.child_program)
Expand Down
28 changes: 23 additions & 5 deletions tests/test_main.py
@@ -1,4 +1,4 @@
# Copyright 2015-2018 Facundo Batista, Nicolás Demarchi
# Copyright 2015-2019 Facundo Batista, Nicolás Demarchi
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License version 3, as published
Expand All @@ -16,12 +16,13 @@

"""Tests for some code in main."""

import os
import unittest
from unittest.mock import patch

from pkg_resources import Requirement

from fades import main, parsing, __version__, VERSION
from fades import VERSION, FadesError, __version__, main, parsing
from tests import create_tempfile


Expand Down Expand Up @@ -176,9 +177,21 @@ def test_no_child_at_all(self):
self.assertIsNone(child)

def test_normal_child_program(self):
analyzable, child = main.decide_child_program(False, "foobar.py")
self.assertEqual(analyzable, "foobar.py")
self.assertEqual(child, "foobar.py")
child_path = create_tempfile(self, "")
analyzable, child = main.decide_child_program(False, child_path)
self.assertEqual(analyzable, child_path)
self.assertEqual(child, child_path)

def test_normal_child_program_not_found(self):
with self.assertRaises(FadesError):
main.decide_child_program(False, 'does_not_exist.py')

def test_normal_child_program_no_access(self):
child_path = create_tempfile(self, "")
os.chmod(child_path, 333) # Remove read permission.
self.addCleanup(os.chmod, child_path, 333)
with self.assertRaises(FadesError):
main.decide_child_program(False, 'does_not_exist.py')

def test_remote_child_program_simple(self):
with patch('fades.helpers.download_remote_script') as mock:
Expand All @@ -199,3 +212,8 @@ def test_remote_child_program_ssl(self):
# check that analyzable and child are the same, and that its content is the remote one
self.assertEqual(analyzable, "new_path_script")
self.assertEqual(child, "new_path_script")

def test_indicated_with_executable_flag_in_path(self):
"""Absolute paths not allowed when using --exec."""
with self.assertRaises(FadesError):
main.decide_child_program(True, "/path/foobar.py")

0 comments on commit cb27d27

Please sign in to comment.