Skip to content

Commit

Permalink
Merge b68e3da into master
Browse files Browse the repository at this point in the history
  • Loading branch information
github-actions[bot] authored May 27, 2021
2 parents 78be090 + b68e3da commit 7c089a7
Show file tree
Hide file tree
Showing 23 changed files with 214 additions and 84 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def load_module_dict(filename: str) -> dict:


name = "vien"
constants = load_module_dict(f'{name}/constants.py')
constants = load_module_dict(f'{name}/_constants.py')

readme = (Path(__file__).parent / 'README.md').read_text(encoding="utf-8")
readme = "# " + readme.partition("\n#")[-1]
Expand Down
7 changes: 5 additions & 2 deletions tests/test_arg_parser.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
# SPDX-FileCopyrightText: (c) 2021 Artëm IG <github.com/rtmigo>
# SPDX-License-Identifier: BSD-3-Clause

import unittest
from pathlib import Path
from typing import List

from vien._common import is_windows

from tests.common import is_posix
from vien.main import get_project_dir
from vien.arg_parser import Parsed, Commands, items_after
from vien._main import get_project_dir
from vien._parsed_args import Parsed, Commands, items_after


def windows_too(args: List[str]) -> List[str]:
Expand Down
2 changes: 1 addition & 1 deletion tests/bash_runner_test.py → tests/test_bash_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from timeit import default_timer as timer

from tests.common import is_posix
from vien.bash_runner import *
from vien._bash_runner import *
from tests.time_limited import TimeLimited


Expand Down
107 changes: 107 additions & 0 deletions tests/test_call_funcs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
# SPDX-FileCopyrightText: (c) 2021 Artëm IG <github.com/rtmigo>
# SPDX-License-Identifier: BSD-3-Clause

import os
import unittest

from vien._call_funcs import relative_fn_to_module_name, NotInnerPath, \
relative_inner_path


def rip(a: str, b: str) -> str:
if os.name == 'posix':
a = a.replace("W:", "")
b = b.replace("W:", "")
return relative_inner_path(a, b)


class TestRelativeInnerPath(unittest.TestCase):

def assertEqualAnyslash(self, a: str, b: str):
self.assertEqual(a.replace('\\', '/'),
b.replace('\\', '/'))

def test_child(self):
self.assertEqualAnyslash(
rip('W:/abc/myProject/file.py', 'W:/abc/myProject'),
'file.py')

def test_sub_child(self):
self.assertEqualAnyslash(
rip('W:/abc/myProject/pkg/sub/file.py',
'W:/abc/myProject'),
'pkg/sub/file.py')

@unittest.skipUnless(os.name == 'nt', "windows-specific")
def test_sub_child_back(self):
self.assertEqualAnyslash(
rip('W:\\abc\\myProject\\pkg\\sub\\file.py',
'W:\\abc\\myProject'),
'pkg/sub/file.py')

def test_both_relative(self):
self.assertEqualAnyslash(
rip('myProject/pkg/sub/file.py',
'myProject'),
'pkg/sub/file.py')

def test_same(self):
with self.assertRaises(NotInnerPath):
rip('W:/abc/myProject/x', 'W:/abc/myProject/x'),

def test_swapped(self):
with self.assertRaises(NotInnerPath):
rip('W:/abc', 'W:/abc/myProject/x'),

@unittest.skipUnless(os.name == 'nt', "windows-specific")
def test_swapped_backslash(self):
with self.assertRaises(NotInnerPath):
rip('W:\\abc', 'W:\\abc\\myProject\\x'),

def test_neighbor(self):
with self.assertRaises(NotInnerPath):
rip('W:/abc', 'W:/abc/myProject/x'),


class TestFnToModuleName(unittest.TestCase):

def test_fwd(self):
self.assertEqual(
relative_fn_to_module_name('pkg/sub/module.py'),
'pkg.sub.module')

def test_py_uppercase(self):
self.assertEqual(
relative_fn_to_module_name('pkg/sub/module.PY'),
'pkg.sub.module')

def test_no_slashes(self):
self.assertEqual(
relative_fn_to_module_name('file.py'),
'file')

@unittest.skipUnless(os.name == 'nt', "windows-specific")
def test_back(self):
self.assertEqual(
relative_fn_to_module_name('pkg\\sub\\module.py'),
'pkg.sub.module')

def test_no_py(self):
with self.assertRaises(ValueError):
relative_fn_to_module_name('pkg/sub/module')

def test_dots_ext(self):
with self.assertRaises(ValueError):
relative_fn_to_module_name('pkg/sub/module.ext.py')

def test_dots_parent(self):
with self.assertRaises(ValueError):
relative_fn_to_module_name('../sub/module.ext.py')

def test_dots_name(self):
with self.assertRaises(ValueError):
relative_fn_to_module_name('sub/.module.py')


if __name__ == "__main__":
unittest.main()
36 changes: 6 additions & 30 deletions tests/test_call_parser.py
Original file line number Diff line number Diff line change
@@ -1,35 +1,11 @@
# SPDX-FileCopyrightText: (c) 2021 Artëm IG <github.com/rtmigo>
# SPDX-License-Identifier: BSD-3-Clause

import unittest

from tests.common import is_posix
from vien.call_parser import ParsedCall


# class TestOld(unittest.TestCase):
# # remove?
# def test_items_after(self):
# self.assertEqual(list(items_after(['A', 'B', 'C'], 'A')),
# ['B', 'C'])
# self.assertEqual(list(items_after(['A', 'B', 'C'], 'B')),
# ['C'])
# self.assertEqual(list(items_after(['A', 'B', 'C'], 'C')),
# [])
# with self.assertRaises(LookupError):
# list(items_after(['A', 'B', 'C'], 'X'))
#
# def test_call_pyfile(self):
# self.assertEqual(
# call_pyfile("vien -p zzz call -d file.py arg1".split()),
# "file.py")
# self.assertEqual(
# call_pyfile("vien -p zzz call -d arg1 arg2".split()),
# None)
# self.assertEqual(
# call_pyfile("vien -p zzz call -d File.PY arg1".split()),
# "File.PY")
# self.assertEqual(
# call_pyfile("vien aaa.py bbb.py call -d ccc.py arg1".split()),
# "ccc.py")
from vien.exceptions import PyFileArgNotFoundExit
from vien._parsed_call import ParsedCall

from vien._exceptions import PyFileArgNotFoundExit


class TestNew(unittest.TestCase):
Expand Down
9 changes: 6 additions & 3 deletions tests/test_get_project_dir.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
# SPDX-FileCopyrightText: (c) 2021 Artëm IG <github.com/rtmigo>
# SPDX-License-Identifier: BSD-3-Clause

import os
import unittest
from pathlib import Path

from tests.common import is_posix
from tests.test_arg_parser import windows_too
from vien.main import get_project_dir
from vien.exceptions import PyFileArgNotFoundExit
from vien.arg_parser import Parsed
from vien._main import get_project_dir
from vien._exceptions import PyFileArgNotFoundExit
from vien._parsed_args import Parsed


def fix_paths(s: str):
Expand Down
4 changes: 2 additions & 2 deletions tests/main_test.py → tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@
from typing import List, Optional

from tests.test_arg_parser import windows_too
from vien.arg_parser import Parsed
from vien._parsed_args import Parsed

from vien._common import is_windows

from tests.common import is_posix
from tests.time_limited import TimeLimited
from vien import main_entry_point
from vien.exceptions import ChildExit, VenvExistsExit, VenvDoesNotExistExit, \
from vien._exceptions import ChildExit, VenvExistsExit, VenvDoesNotExistExit, \
PyFileNotFoundExit, FailedToCreateVenvExit, CannotFindExecutableExit


Expand Down
5 changes: 4 additions & 1 deletion tests/pythonpath_test.py → tests/test_pythonpath.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
# SPDX-FileCopyrightText: (c) 2021 Artëm IG <github.com/rtmigo>
# SPDX-License-Identifier: BSD-3-Clause

import os
import unittest

from vien.main import _insert_into_pythonpath
from vien._main import _insert_into_pythonpath


@unittest.skipUnless(os.name == 'posix', "posix colons format")
Expand Down
3 changes: 3 additions & 0 deletions tests/systems_test.py → tests/test_systems.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# SPDX-FileCopyrightText: (c) 2021 Artëm IG <github.com/rtmigo>
# SPDX-License-Identifier: BSD-3-Clause

import unittest

from vien._common import need_posix, UnexpectedOsError, need_windows
Expand Down
5 changes: 4 additions & 1 deletion tests/vien_dir_test.py → tests/test_vien_dir.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
# SPDX-FileCopyrightText: (c) 2021 Artëm IG <github.com/rtmigo>
# SPDX-License-Identifier: BSD-3-Clause

import os
import unittest
from pathlib import Path

from tests.common import is_posix
from vien.main import get_vien_dir
from vien._main import get_vien_dir

@unittest.skipUnless(is_posix, "not POSIX")
class TestVenvsDir(unittest.TestCase):
Expand Down
3 changes: 3 additions & 0 deletions tests/time_limited.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# SPDX-FileCopyrightText: (c) 2021 Artëm IG <github.com/rtmigo>
# SPDX-License-Identifier: BSD-3-Clause

import signal


Expand Down
7 changes: 5 additions & 2 deletions vien/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
from .constants import __version__, __license__, __copyright__
# SPDX-FileCopyrightText: (c) 2021 Artëm IG <github.com/rtmigo>
# SPDX-License-Identifier: BSD-3-Clause

from ._constants import __version__, __license__, __copyright__
from ._common import is_posix
from .main import main_entry_point
from ._main import main_entry_point

5 changes: 4 additions & 1 deletion vien/__main__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
from vien.main import main_entry_point
# SPDX-FileCopyrightText: (c) 2021 Artëm IG <github.com/rtmigo>
# SPDX-License-Identifier: BSD-3-Clause

from vien._main import main_entry_point

if __name__ == "__main__":
main_entry_point()
File renamed without changes.
38 changes: 38 additions & 0 deletions vien/_call_funcs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# SPDX-FileCopyrightText: (c) 2021 Artëm IG <github.com/rtmigo>
# SPDX-License-Identifier: BSD-3-Clause

import os
from pathlib import Path
from typing import Union


def relative_fn_to_module_name(filename: str) -> str:
if not filename.lower().endswith('.py'):
raise ValueError("The filename does not end with '.py'.")
filename = filename[:-3]
if '.' in filename:
raise ValueError("The filename contains dots.")
if os.name == "nt":
filename = filename.replace('\\', '/')
assert not os.path.isabs(filename)
# assert not filename.split()[0] == ".."
return filename.replace('/', '.')


class NotInnerPath(ValueError):
pass


def relative_inner_path(child: Union[str, Path],
parent: Union[str, Path]) -> str:
"""(/abc/parent/xyz/child, /abc/parent) -> xyz/child
Not only returns the "relative" path, but also checks
it is really relative.
"""
rel_path = os.path.relpath(child, parent)

first = rel_path.split(os.path.sep)[0]
print(first)
if first == ".." or first == "." or os.path.isabs(rel_path):
raise NotInnerPath(f"The {child} is not a child of {parent}.")
return rel_path
4 changes: 2 additions & 2 deletions vien/escaping_cmd.py → vien/_cmdexe_escape_args.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ def cmd_escape_arg(arg):
if not arg or re.search(r'(["\s])', arg):
arg = '"' + arg.replace('"', r'\"') + '"'

return _escape_for_cmd_exe(arg)
return _inner_escape(arg)


def _escape_for_cmd_exe(arg):
def _inner_escape(arg):
# Escape an argument string to be suitable to be passed to
# cmd.exe on Windows
#
Expand Down
File renamed without changes.
4 changes: 4 additions & 0 deletions vien/_common.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# SPDX-FileCopyrightText: (c) 2021 Artëm IG <github.com/rtmigo>
# SPDX-License-Identifier: BSD-3-Clause


import os

is_windows = os.name == 'nt'
Expand Down
2 changes: 1 addition & 1 deletion vien/constants.py → vien/_constants.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
__version__ = "8.0.1"
__version__ = "8.0.2"
__copyright__ = "(c) 2020-2021 Artëm IG <github.com/rtmigo>"
__license__ = "BSD-3-Clause"
8 changes: 3 additions & 5 deletions vien/exceptions.py → vien/_exceptions.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
from pathlib import Path


# from vien.main import exe_name

# SPDX-FileCopyrightText: (c) 2021 Artëm IG <github.com/rtmigo>
# SPDX-License-Identifier: BSD-3-Clause

from pathlib import Path


class VienExit(SystemExit):
Expand Down
Loading

0 comments on commit 7c089a7

Please sign in to comment.