Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor core.py and add unit tests #10

Merged
merged 4 commits into from Mar 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions .circleci/config.yml
Expand Up @@ -13,6 +13,12 @@ jobs:
. venv/bin/activate
pip install --requirement dev_requirements.txt
./dev-scripts/build
- run:
name: install coveralls and upload coverage information
command: |
. venv/bin/activate
pip install coveralls==3.0.1
coveralls
install:
docker:
- image: circleci/python:3.7.3
Expand Down
2 changes: 1 addition & 1 deletion README.md
@@ -1,6 +1,6 @@
# pyrestic

[![CircleCI](https://circleci.com/gh/mtlynch/pyrestic.svg?style=svg)](https://circleci.com/gh/mtlynch/pyrestic) [![License](http://img.shields.io/:license-mit-blue.svg?style=flat-square)](LICENSE)
[![CircleCI](https://circleci.com/gh/mtlynch/pyrestic.svg?style=svg)](https://circleci.com/gh/mtlynch/pyrestic) [![Coverage Status](https://coveralls.io/repos/github/mtlynch/pyrestic/badge.svg?branch=master)](https://coveralls.io/github/mtlynch/pyrestic?branch=master) [![License](http://img.shields.io/:license-mit-blue.svg?style=flat-square)](LICENSE)

## Overview

Expand Down
7 changes: 7 additions & 0 deletions dev-scripts/build
Expand Up @@ -22,6 +22,13 @@ find . \
-not -path "./${VIRTUALENV_DIR}/*" \
-delete

# Run unit tests and calculate code coverage.
coverage run \
--source "$SOURCE_DIR" \
--omit "*_test.py" \
-m unittest discover --pattern "*_test.py"
coverage report

# Check that source has correct formatting.
yapf \
--diff \
Expand Down
1 change: 1 addition & 0 deletions dev_requirements.txt
@@ -1,3 +1,4 @@
coverage==5.5
isort[requirements_deprecated_finder]==5.7.0
pyflakes==2.2.0
yapf==0.31.0
10 changes: 10 additions & 0 deletions restic/__init__.py
@@ -0,0 +1,10 @@
from restic.internal import self_update as internal_self_update
from restic.internal import version as internal_version


def self_update():
return internal_self_update.run()


def version():
return internal_version.run()
86 changes: 0 additions & 86 deletions restic/core.py

This file was deleted.

25 changes: 25 additions & 0 deletions restic/generate.py
@@ -0,0 +1,25 @@
from restic.config import restic_bin
from restic.internal import command_executor


def generate(bash_completion=None, man=None, zsh_completion=None):
cmd = [restic_bin, 'generate']
if bash_completion is not None:
if type(bash_completion) == str:
cmd.extend(['--bash-completion', bash_completion])
else:
raise ValueError('bash-completion shall be type of str or None')

if man is not None:
if type(man) == str:
cmd.extend(['--man', man])
else:
raise ValueError('man shall be type of str or None')

if zsh_completion is not None:
if type(zsh_completion) == str:
cmd.extend(['--zsh-completion', zsh_completion])
else:
raise ValueError('zsh-completion shall be type of str or None')

command_executor.execute(cmd)
Empty file added restic/internal/__init__.py
Empty file.
22 changes: 22 additions & 0 deletions restic/internal/command_executor.py
@@ -0,0 +1,22 @@
import subprocess


def execute(cmd):
out = ''
err = ''
try:
with subprocess.Popen(cmd,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
encoding='utf-8',
text=True) as proc:
out, err = proc.communicate()
if err is not None:
raise RuntimeError('Command runtime failure')
proc.wait()
if proc.returncode != 0:
raise RuntimeError(f'Return code {proc.returncode} is not zero')
except FileNotFoundError:
raise RuntimeError('Cannot find restic installed')

return out
7 changes: 7 additions & 0 deletions restic/internal/self_update.py
@@ -0,0 +1,7 @@
from restic.config import restic_bin
from restic.internal import command_executor


def run():
cmd = [restic_bin, 'self-update']
command_executor.execute(cmd)
13 changes: 13 additions & 0 deletions restic/internal/self_update_test.py
@@ -0,0 +1,13 @@
import unittest
from unittest import mock

import restic
from restic.internal import self_update


class SelfUpdateTest(unittest.TestCase):

@mock.patch.object(self_update.command_executor, 'execute')
def test_self_update(self, mock_execute):
restic.self_update()
mock_execute.assert_called_with(['restic', 'self-update'])
18 changes: 18 additions & 0 deletions restic/internal/version.py
@@ -0,0 +1,18 @@
import re

from restic import config
from restic.internal import command_executor


def run():
cmd = [config.restic_bin, 'version']
out = command_executor.execute(cmd)
match = re.match(
r'restic ([0-9\.]+) compiled with go([0-9\.]+) on ([a-zA-Z0-9]+)/([a-zA-Z0-9]+)',
out)
return {
'restic_version': match.group(1),
'go_version': match.group(2),
'platform_version': match.group(3),
'architecture': match.group(4)
}
23 changes: 23 additions & 0 deletions restic/internal/version_test.py
@@ -0,0 +1,23 @@
import unittest
from unittest import mock

import restic
from restic.internal import version


class VersionTest(unittest.TestCase):

@mock.patch.object(version.command_executor, 'execute')
def test_version(self, mock_execute):
mock_execute.return_value = (
'restic 0.12.0 compiled with go1.15.8 on windows/amd64')

self.assertEqual(
{
'architecture': 'amd64',
'go_version': '1.15.8',
'platform_version': 'windows',
'restic_version': '0.12.0'
}, restic.version())

mock_execute.assert_called_with(['restic', 'version'])