Add no .env exception

This commit is contained in:
2022-05-08 16:51:20 +02:00
parent 2c8646c8d8
commit 1db5d8723a
5 changed files with 46 additions and 15 deletions

View File

@@ -1,6 +1,7 @@
import sys
import argparse import argparse
from .Experiments import Models from .Experiments import Models
from .Utils import Files from .Utils import Files, NO_ENV
ALL_METRICS = ( ALL_METRICS = (
"accuracy", "accuracy",
@@ -15,13 +16,18 @@ class EnvData:
@staticmethod @staticmethod
def load(): def load():
args = {} args = {}
with open(Files.dot_env) as f: try:
for line in f.read().splitlines(): with open(Files.dot_env) as f:
if line == "" or line.startswith("#"): for line in f.read().splitlines():
continue if line == "" or line.startswith("#"):
key, value = line.split("=") continue
args[key] = value key, value = line.split("=")
return args args[key] = value
except FileNotFoundError:
print(NO_ENV, file=sys.stderr)
exit(1)
else:
return args
class EnvDefault(argparse.Action): class EnvDefault(argparse.Action):

View File

@@ -3,6 +3,7 @@ import subprocess
BEST_ACCURACY_STREE = 40.282203 BEST_ACCURACY_STREE = 40.282203
NO_RESULTS = "** No results found **" NO_RESULTS = "** No results found **"
NO_ENV = "File .env not found"
class Folders: class Folders:

View File

@@ -1,7 +1,8 @@
import os
from io import StringIO from io import StringIO
from unittest.mock import patch from unittest.mock import patch
from .TestBase import TestBase from .TestBase import TestBase
from ..Arguments import Arguments, ALL_METRICS from ..Arguments import Arguments, ALL_METRICS, NO_ENV
class ArgumentsTest(TestBase): class ArgumentsTest(TestBase):
@@ -65,23 +66,35 @@ class ArgumentsTest(TestBase):
self.assertEqual(args.key, "metric") self.assertEqual(args.key, "metric")
@patch("sys.stderr", new_callable=StringIO) @patch("sys.stderr", new_callable=StringIO)
def test_xset_mandatory(self, mock_stderr): def test_xset_mandatory(self, stderr):
arguments = self.build_args() arguments = self.build_args()
test_args = ["-n", "3", "-k", "date"] test_args = ["-n", "3", "-k", "date"]
with self.assertRaises(SystemExit): with self.assertRaises(SystemExit):
arguments.parse(test_args) arguments.parse(test_args)
self.assertRegexpMatches( self.assertRegexpMatches(
mock_stderr.getvalue(), stderr.getvalue(),
r"error: the following arguments are required: -m/--model", r"error: the following arguments are required: -m/--model",
) )
@patch("sys.stderr", new_callable=StringIO) @patch("sys.stderr", new_callable=StringIO)
def test_xset_required(self, mock_stderr): def test_xset_required(self, stderr):
arguments = self.build_args() arguments = self.build_args()
test_args = ["-n", "3", "-m", "SVC"] test_args = ["-n", "3", "-m", "SVC"]
with self.assertRaises(SystemExit): with self.assertRaises(SystemExit):
arguments.parse(test_args) arguments.parse(test_args)
self.assertRegexpMatches( self.assertRegexpMatches(
mock_stderr.getvalue(), stderr.getvalue(),
r"error: the following arguments are required: -k/--key", 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")

View File

@@ -2,7 +2,6 @@ from io import StringIO
from unittest.mock import patch from unittest.mock import patch
from .TestBase import TestBase from .TestBase import TestBase
from ..Results import Summary from ..Results import Summary
from ..Utils import NO_RESULTS
class SummaryTest(TestBase): class SummaryTest(TestBase):
@@ -229,4 +228,4 @@ class SummaryTest(TestBase):
report.acquire() report.acquire()
with self.assertRaises(ValueError) as msg: with self.assertRaises(ValueError) as msg:
report.list_results(score="f1-macro", model="STree") report.list_results(score="f1-macro", model="STree")
self.assertEqual(str(msg.exception), NO_RESULTS) self.assertEqual(str(msg.exception), "** No results found **")

View File

@@ -44,3 +44,15 @@ class BeListTest(TestBase):
stdout, stderr = self.execute_script("be_list", ["--nan", "1"]) stdout, stderr = self.execute_script("be_list", ["--nan", "1"])
self.assertEqual(stderr.getvalue(), "") self.assertEqual(stderr.getvalue(), "")
self.check_output_file(stdout, "be_list_no_nan") 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)