Skip to content

Commit

Permalink
Allow running sinol-make from package's subdirectory (#129)
Browse files Browse the repository at this point in the history
* Allow running `sinol-make` in subdirectory of package

(cherry picked from commit 8151dba)

* Add test

(cherry picked from commit 902abc4)

* Refactor

* Fix tests
  • Loading branch information
MasloMaslane authored Sep 24, 2023
1 parent b70cfa2 commit 13351c2
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 9 deletions.
24 changes: 15 additions & 9 deletions src/sinol_make/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,28 @@ def get_commands():
return commands


def check_if_package():
def find_and_chdir_package():
"""
Function to check if current directory is a package
Checks if current directory or parent directory is a package directory.
If it is, it changes the current working directory to it and returns True.
If it isn't, it returns False.
"""

cwd = os.getcwd()
if os.path.exists(os.path.join(cwd, 'config.yml')):
if os.path.exists(os.path.join(os.getcwd(), 'config.yml')):
return True
elif os.path.exists(os.path.join(os.getcwd(), '..', 'config.yml')):
os.chdir('..')
return True
return False
else:
return False


def exit_if_not_package():
"""
Function that exits if current directory is not a package
Checks if current directory or parent directory is a package directory.
If it is, current working directory is changed to it.
If it isn't, it exits with an error.
"""
if not check_if_package():
if not find_and_chdir_package():
exit_with_error('You are not in a package directory (couldn\'t find config.yml in current directory).')


Expand Down Expand Up @@ -292,7 +298,7 @@ def make_version_changes():
# In version 1.5.9 we changed the format of sinol_expected_scores.
# Now all groups have specified points and status.

if check_if_package():
if find_and_chdir_package():
with open("config.yml", "r") as config_file:
config = yaml.load(config_file, Loader=yaml.FullLoader)

Expand Down
14 changes: 14 additions & 0 deletions tests/commands/run/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -632,3 +632,17 @@ def test(file_to_change, lang, comment_character):
test("liblib.cpp", "cpp", "//")
test("liblib.h", "cpp", "//")
test("liblib.py", "py", "#")


@pytest.mark.parametrize("create_package", [get_simple_package_path()], indirect=True)
def test_cwd_in_prog(create_package):
"""
Test if `sinol-make` works when cwd is in prog.
"""
package_path = create_package
os.chdir("prog")
create_ins_outs(package_path)
parser = configure_parsers()
args = parser.parse_args(["run"])
command = Command()
command.run(args)

0 comments on commit 13351c2

Please sign in to comment.