From 1db5d8723a96e6b5ca1672f5b03d9609ed235fb5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ricardo=20Montan=CC=83ana?= Date: Sun, 8 May 2022 16:51:20 +0200 Subject: [PATCH] Add no .env exception --- benchmark/Arguments.py | 22 ++++++++++++++-------- benchmark/Utils.py | 1 + benchmark/tests/Arguments_test.py | 23 ++++++++++++++++++----- benchmark/tests/Summary_test.py | 3 +-- benchmark/tests/scripts/Be_List_test.py | 12 ++++++++++++ 5 files changed, 46 insertions(+), 15 deletions(-) diff --git a/benchmark/Arguments.py b/benchmark/Arguments.py index 1446099..a034934 100644 --- a/benchmark/Arguments.py +++ b/benchmark/Arguments.py @@ -1,6 +1,7 @@ +import sys import argparse from .Experiments import Models -from .Utils import Files +from .Utils import Files, NO_ENV ALL_METRICS = ( "accuracy", @@ -15,13 +16,18 @@ class EnvData: @staticmethod def load(): args = {} - with open(Files.dot_env) as f: - for line in f.read().splitlines(): - if line == "" or line.startswith("#"): - continue - key, value = line.split("=") - args[key] = value - return args + try: + with open(Files.dot_env) as f: + for line in f.read().splitlines(): + if line == "" or line.startswith("#"): + continue + key, value = line.split("=") + args[key] = value + except FileNotFoundError: + print(NO_ENV, file=sys.stderr) + exit(1) + else: + return args class EnvDefault(argparse.Action): diff --git a/benchmark/Utils.py b/benchmark/Utils.py index 57a646f..176352a 100644 --- a/benchmark/Utils.py +++ b/benchmark/Utils.py @@ -3,6 +3,7 @@ import subprocess BEST_ACCURACY_STREE = 40.282203 NO_RESULTS = "** No results found **" +NO_ENV = "File .env not found" class Folders: diff --git a/benchmark/tests/Arguments_test.py b/benchmark/tests/Arguments_test.py index 4e9768f..312bed1 100644 --- a/benchmark/tests/Arguments_test.py +++ b/benchmark/tests/Arguments_test.py @@ -1,7 +1,8 @@ +import os from io import StringIO from unittest.mock import patch from .TestBase import TestBase -from ..Arguments import Arguments, ALL_METRICS +from ..Arguments import Arguments, ALL_METRICS, NO_ENV class ArgumentsTest(TestBase): @@ -65,23 +66,35 @@ class ArgumentsTest(TestBase): self.assertEqual(args.key, "metric") @patch("sys.stderr", new_callable=StringIO) - def test_xset_mandatory(self, mock_stderr): + def test_xset_mandatory(self, stderr): arguments = self.build_args() test_args = ["-n", "3", "-k", "date"] with self.assertRaises(SystemExit): arguments.parse(test_args) self.assertRegexpMatches( - mock_stderr.getvalue(), + stderr.getvalue(), r"error: the following arguments are required: -m/--model", ) @patch("sys.stderr", new_callable=StringIO) - def test_xset_required(self, mock_stderr): + def test_xset_required(self, stderr): arguments = self.build_args() test_args = ["-n", "3", "-m", "SVC"] with self.assertRaises(SystemExit): arguments.parse(test_args) self.assertRegexpMatches( - mock_stderr.getvalue(), + stderr.getvalue(), r"error: the following arguments are required: -k/--key", ) + + @patch("sys.stderr", new_callable=StringIO) + def test_no_env(self, stderr): + path = os.getcwd() + os.chdir("..") + try: + self.build_args() + except SystemExit: + pass + finally: + os.chdir(path) + self.assertEqual(stderr.getvalue(), f"{NO_ENV}\n") diff --git a/benchmark/tests/Summary_test.py b/benchmark/tests/Summary_test.py index 8d68abc..6b43344 100644 --- a/benchmark/tests/Summary_test.py +++ b/benchmark/tests/Summary_test.py @@ -2,7 +2,6 @@ from io import StringIO from unittest.mock import patch from .TestBase import TestBase from ..Results import Summary -from ..Utils import NO_RESULTS class SummaryTest(TestBase): @@ -229,4 +228,4 @@ class SummaryTest(TestBase): report.acquire() with self.assertRaises(ValueError) as msg: report.list_results(score="f1-macro", model="STree") - self.assertEqual(str(msg.exception), NO_RESULTS) + self.assertEqual(str(msg.exception), "** No results found **") diff --git a/benchmark/tests/scripts/Be_List_test.py b/benchmark/tests/scripts/Be_List_test.py index a06e429..9ccaf78 100644 --- a/benchmark/tests/scripts/Be_List_test.py +++ b/benchmark/tests/scripts/Be_List_test.py @@ -44,3 +44,15 @@ class BeListTest(TestBase): stdout, stderr = self.execute_script("be_list", ["--nan", "1"]) self.assertEqual(stderr.getvalue(), "") self.check_output_file(stdout, "be_list_no_nan") + + def test_be_no_env(self): + path = os.getcwd() + os.chdir("..") + stderr = None + try: + _, stderr = self.execute_script("be_list", []) + except SystemExit as e: + self.assertEqual(e.code, 1) + finally: + os.chdir(path) + self.assertIsNone(stderr)