Skip to content

Commit

Permalink
Merge pull request #63 from DAGsHub/mohithg/fail-tests
Browse files Browse the repository at this point in the history
Fail tests on flake8 failure
  • Loading branch information
guysmoilov committed Jun 21, 2021
2 parents 3ecc41b + ee5e5dc commit c134b8e
Show file tree
Hide file tree
Showing 10 changed files with 43 additions and 41 deletions.
9 changes: 7 additions & 2 deletions .github/workflows/test.yml
Expand Up @@ -34,8 +34,13 @@ jobs:
run: |
# stop the build if there are Python syntax errors or undefined names
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
# The GitHub editor is 127 chars wide and save output to variable
OUTPUT=$(flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics)
echo "$OUTPUT"
# Take last line of output
count=$(echo "$OUTPUT" | tail -1)
# Fail test explicitly if count >= 10
if [[ $count -ge 10 ]]; then exit $count; fi
- name: Setup env for tests
run: |
git config --global user.email "you@example.com"
Expand Down
1 change: 1 addition & 0 deletions fds/domain/commands.py
Expand Up @@ -10,5 +10,6 @@ class Commands(Enum):
PUSH = "push"
SAVE = "save"


class AddCommands(Enum):
ALL = "."
2 changes: 1 addition & 1 deletion fds/domain/constants.py
@@ -1,3 +1,3 @@
import os

MAX_THRESHOLD_SIZE = int(os.getenv('MAX_THRESHOLD_SIZE', 10 * 1024 * 1024)) # 10 MB
MAX_THRESHOLD_SIZE = int(os.getenv('MAX_THRESHOLD_SIZE', 10 * 1024 * 1024)) # 10 MB
1 change: 1 addition & 0 deletions fds/services/pretty_print.py
@@ -1,6 +1,7 @@
from colorama import init
from colorama import Fore


class PrettyPrint(object):

def __init__(self):
Expand Down
6 changes: 2 additions & 4 deletions fds/utils.py
@@ -1,8 +1,6 @@
import argparse
import subprocess
from pathlib import Path
import os
import sys
from typing import List, Union, Any

import humanize
Expand All @@ -25,7 +23,7 @@ def convert_bytes_to_string(bytes_data: bytes) -> str:
return bytes_data.decode("utf-8")


def execute_command(command: Union[str, List[str]], shell: bool = False, capture_output: bool=True,
def execute_command(command: Union[str, List[str]], shell: bool = False, capture_output: bool = True,
ignorable_return_codes: List[int] = [0]) -> Any:
if capture_output:
# capture_output is not available in python 3.6, so using PIPE manually
Expand Down Expand Up @@ -54,7 +52,7 @@ def does_file_exist(filename: str) -> bool:
try:
import os.path
return os.path.exists(filename)
except Exception as e:
except Exception:
return False


Expand Down
6 changes: 3 additions & 3 deletions tests/it/helpers.py
Expand Up @@ -27,7 +27,7 @@ def create_fake_git_data(self):
git_path = f"{self.repo_path}/git_data"
Path(git_path).mkdir(parents=True, exist_ok=True)
# Creating 5 random files
for i in range(0,5):
for i in range(0, 5):
self.create_dummy_file(f"{git_path}/file-{i}", 10)

def create_dummy_file(self, file_name: str, size: int):
Expand All @@ -38,7 +38,7 @@ def create_fake_dvc_data(self):
dvc_path = f"{self.repo_path}/dvc_data"
Path(dvc_path).mkdir(parents=True, exist_ok=True)
# creating a big folder
for i in range(0,101):
for i in range(0, 101):
self.create_dummy_file(f"{dvc_path}/file-{i}", 10)
# creating one large file
self.create_dummy_file(f"large_file", 11 * 1024)
self.create_dummy_file("large_file", 11 * 1024)
20 changes: 10 additions & 10 deletions tests/it/test_dvc.py
@@ -1,10 +1,10 @@
import os
from unittest.mock import patch

from fds.services.dvc_service import DvcChoices
from fds.utils import does_file_exist, execute_command, convert_bytes_to_string
from tests.it.helpers import IntegrationTestCase


class TestDvc(IntegrationTestCase):

def test_init_dvc(self):
Expand All @@ -31,7 +31,7 @@ def test_add(self):
self.dvc_service.init()
super().create_fake_dvc_data()
output = execute_command(["git", "status"], capture_output=True)
assert f"large_file" in convert_bytes_to_string(output.stdout)
assert "large_file" in convert_bytes_to_string(output.stdout)
msg = self.dvc_service.add("large_file")
assert does_file_exist(f"{self.repo_path}/large_file.dvc")
assert msg == "DVC add successfully executed"
Expand All @@ -40,28 +40,28 @@ def test_add_nothing(self):
self.git_service.init()
self.dvc_service.init()
super().create_fake_dvc_data()
msg = self.dvc_service.add(f"dvc_data/file-0")
msg = self.dvc_service.add("dvc_data/file-0")
assert msg == "Nothing to add in DVC"

@patch("fds.services.dvc_service.DVCService._get_choice", return_value={"selection_choice": DvcChoices.IGNORE.value})
def test_add_check_ignore(self, get_choice):
self.fds_service.init()
super().create_fake_dvc_data()
output = execute_command(["git", "status"], capture_output=True)
assert f"large_file" in convert_bytes_to_string(output.stdout)
assert does_file_exist(f".gitignore") is False
assert "large_file" in convert_bytes_to_string(output.stdout)
assert does_file_exist(".gitignore") is False
msg = self.dvc_service.add(".")
assert does_file_exist(f".gitignore") is True
assert does_file_exist(".gitignore") is True
assert msg == "Nothing to add in DVC"
output = execute_command(["git", "status"], capture_output=True)
assert f"large_file" not in convert_bytes_to_string(output.stdout)
assert "large_file" not in convert_bytes_to_string(output.stdout)

@patch("fds.services.dvc_service.DVCService._get_choice", return_value={"selection_choice": DvcChoices.ADD_TO_GIT.value})
def test_add_check_add_git(self, get_choice):
self.fds_service.init()
super().create_fake_dvc_data()
output = execute_command(["git", "status"], capture_output=True)
assert f"large_file" in convert_bytes_to_string(output.stdout)
assert "large_file" in convert_bytes_to_string(output.stdout)
msg = self.dvc_service.add(".")
assert msg == "Nothing to add in DVC"

Expand All @@ -70,11 +70,11 @@ def test_add_check_add_dvc(self, get_choice):
self.fds_service.init()
super().create_fake_dvc_data()
output = execute_command(["git", "status"], capture_output=True)
assert f"large_file" in convert_bytes_to_string(output.stdout)
assert "large_file" in convert_bytes_to_string(output.stdout)
msg = self.dvc_service.add(".")
assert msg == "DVC add successfully executed"
output = execute_command(["git", "status"], capture_output=True)
assert f"large_file.dvc" in convert_bytes_to_string(output.stdout)
assert "large_file.dvc" in convert_bytes_to_string(output.stdout)

@patch("fds.services.dvc_service.DVCService._get_choice", return_value={"selection_choice": DvcChoices.ADD_TO_DVC.value})
def test_commit_auto_confirm(self, get_choice):
Expand Down
14 changes: 6 additions & 8 deletions tests/it/test_fds.py
@@ -1,4 +1,3 @@
import os
from unittest.mock import patch

from fds.services.dvc_service import DvcChoices
Expand All @@ -25,13 +24,13 @@ def test_add(self, get_choice):
self.fds_service.add(".")
output = execute_command(["git", "status"], capture_output=True)
# Check DVC add
assert f"new file: large_file.dvc" in convert_bytes_to_string(output.stdout)
assert "new file: large_file.dvc" in convert_bytes_to_string(output.stdout)
# Check Git add
assert f"new file: git_data/file-0" in convert_bytes_to_string(output.stdout)
assert f"new file: git_data/file-1" in convert_bytes_to_string(output.stdout)
assert f"new file: git_data/file-2" in convert_bytes_to_string(output.stdout)
assert f"new file: git_data/file-3" in convert_bytes_to_string(output.stdout)
assert f"new file: git_data/file-4" in convert_bytes_to_string(output.stdout)
assert "new file: git_data/file-0" in convert_bytes_to_string(output.stdout)
assert "new file: git_data/file-1" in convert_bytes_to_string(output.stdout)
assert "new file: git_data/file-2" in convert_bytes_to_string(output.stdout)
assert "new file: git_data/file-3" in convert_bytes_to_string(output.stdout)
assert "new file: git_data/file-4" in convert_bytes_to_string(output.stdout)

@patch("fds.services.dvc_service.DVCService._get_choice", return_value={"selection_choice": DvcChoices.ADD_TO_DVC.value})
def test_commit(self, get_choice):
Expand All @@ -58,4 +57,3 @@ def test_commit_git(self):
self.fds_service.commit("Commit 1", False)
output = execute_command(["git", "log", "--oneline"], capture_output=True)
assert "Commit 1" in convert_bytes_to_string(output.stdout)

17 changes: 8 additions & 9 deletions tests/it/test_git.py
Expand Up @@ -23,19 +23,19 @@ def test_add(self):
self.git_service.add(".")
output = execute_command(["git", "status"], capture_output=True)
assert convert_bytes_to_string(output.stderr) == ""
assert f"new file: git_data/file-0" in convert_bytes_to_string(output.stdout)
assert f"new file: git_data/file-1" in convert_bytes_to_string(output.stdout)
assert f"new file: git_data/file-2" in convert_bytes_to_string(output.stdout)
assert f"new file: git_data/file-3" in convert_bytes_to_string(output.stdout)
assert f"new file: git_data/file-4" in convert_bytes_to_string(output.stdout)
assert "new file: git_data/file-0" in convert_bytes_to_string(output.stdout)
assert "new file: git_data/file-1" in convert_bytes_to_string(output.stdout)
assert "new file: git_data/file-2" in convert_bytes_to_string(output.stdout)
assert "new file: git_data/file-3" in convert_bytes_to_string(output.stdout)
assert "new file: git_data/file-4" in convert_bytes_to_string(output.stdout)

def test_add_one(self):
self.git_service.init()
super().create_fake_git_data()
self.git_service.add("git_data/file-0")
output = execute_command(["git", "status"], capture_output=True)
assert convert_bytes_to_string(output.stderr) == ""
assert f"new file: git_data/file-0" in convert_bytes_to_string(output.stdout)
assert "new file: git_data/file-0" in convert_bytes_to_string(output.stdout)
assert "Untracked files:" in convert_bytes_to_string(output.stdout)
assert "file-1" in convert_bytes_to_string(output.stdout)
assert "file-2" in convert_bytes_to_string(output.stdout)
Expand All @@ -48,8 +48,8 @@ def test_add_gitignore(self):
super().create_dummy_file(".gitignore", 100)
self.git_service.add("git_data/file-0")
output = execute_command(["git", "status"], capture_output=True)
assert f"new file: git_data/file-0" in convert_bytes_to_string(output.stdout)
assert f"new file: .gitignore" in convert_bytes_to_string(output.stdout)
assert "new file: git_data/file-0" in convert_bytes_to_string(output.stdout)
assert "new file: .gitignore" in convert_bytes_to_string(output.stdout)

def test_commit(self):
self.git_service.init()
Expand All @@ -58,4 +58,3 @@ def test_commit(self):
self.git_service.commit("Commit 1")
output = execute_command(["git", "log", "--oneline"], capture_output=True)
assert "Commit 1" in convert_bytes_to_string(output.stdout)

8 changes: 4 additions & 4 deletions tests/test_fds.py
Expand Up @@ -56,7 +56,7 @@ def test_add_git_failure(self, mock_git_service, mock_dvc_service):
mock_git_service.add.side_effect = Exception
fds_service = FdsService(mock_git_service, mock_dvc_service)
self.assertRaises(Exception, mock_git_service.add)
with self.assertRaises(Exception) as cm:
with self.assertRaises(Exception):
fds_service.add(".")
assert mock_git_service.add.called
assert mock_dvc_service.add.called
Expand All @@ -66,7 +66,7 @@ def test_add_git_failure(self, mock_git_service, mock_dvc_service):
def test_add_dvc_failure(self, mock_git_service, mock_dvc_service):
mock_dvc_service.add.side_effect = Exception
fds_service = FdsService(mock_git_service, mock_dvc_service)
with self.assertRaises(Exception) as cm:
with self.assertRaises(Exception):
fds_service.add(".")
self.assertRaises(Exception, mock_dvc_service.add)
assert mock_dvc_service.add.called
Expand All @@ -85,7 +85,7 @@ def test_commit_success(self, mock_git_service, mock_dvc_service):
def test_commit_git_failure(self, mock_git_service, mock_dvc_service):
mock_git_service.commit.side_effect = Exception
fds_service = FdsService(mock_git_service, mock_dvc_service)
with self.assertRaises(Exception) as cm:
with self.assertRaises(Exception):
fds_service.commit("some commit message", True)
self.assertRaises(Exception, mock_git_service.commit)
assert mock_git_service.commit.called
Expand All @@ -96,7 +96,7 @@ def test_commit_git_failure(self, mock_git_service, mock_dvc_service):
def test_commit_dvc_failure(self, mock_git_service, mock_dvc_service):
mock_dvc_service.commit.side_effect = Exception
fds_service = FdsService(mock_git_service, mock_dvc_service)
with self.assertRaises(Exception) as cm:
with self.assertRaises(Exception):
fds_service.commit("some commit message", False)
self.assertRaises(Exception, mock_dvc_service.commit)
assert mock_dvc_service.commit.called
Expand Down

0 comments on commit c134b8e

Please sign in to comment.