Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions .github/workflows/verifier.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: Cram Workspace Verfier

on: [push, pull_request]

jobs:
build:

runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.7", "3.8", "3.9"]

steps:
- uses: actions/checkout@v2
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
sudo apt-get install -y bash
sudo apt-get install -u zsh
python -m pip install --upgrade pip
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
- name: Full verification
run: |
tox
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.tox
*.orig
*.rej
*~
Expand Down
67 changes: 0 additions & 67 deletions .travis.yml

This file was deleted.

2 changes: 1 addition & 1 deletion MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
include .coveragerc .pylintrc .travis.yml Makefile MANIFEST.in
include .coveragerc .pylintrc .travis.yml Makefile MANIFEST.in tox.ini
include *.md *.rst *.txt contrib/* scripts/*
exclude contrib/PKGBUILD
recursive-include examples *.t
Expand Down
5 changes: 1 addition & 4 deletions cram/_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,9 @@ def testwrapper():
if not quiet:
_log('\n', None, verbose)

errfile = open(errpath, 'wb')
try:
with open(errpath, 'wb') as errfile:
for line in postout:
errfile.write(line)
finally:
errfile.close()

if not quiet:
origdiff = diff
Expand Down
22 changes: 17 additions & 5 deletions cram/_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import os
import sys

from contextlib import contextmanager

from cram._test import testfile

__all__ = ['runtests']
Expand Down Expand Up @@ -30,6 +32,20 @@ def _findtests(paths):
else:
yield os.path.normpath(p)

@contextmanager
def _cwd(path):
"""Change the working directory

This context manager will change the working directory
and restore it afterwards.
"""
cwd = os.getcwd()
os.chdir(path)
try:
yield path
finally:
os.chdir(cwd)

def runtests(paths, tmpdir, shell, indent=2, cleanenv=True, debug=False):
"""Run tests and yield results.

Expand All @@ -42,7 +58,6 @@ def runtests(paths, tmpdir, shell, indent=2, cleanenv=True, debug=False):

(list of lines in the test, same list with actual output, diff)
"""
cwd = os.getcwd()
seen = set()
basenames = set()
for i, path in enumerate(_findtests(paths)):
Expand All @@ -65,12 +80,9 @@ def test():
"""Run test file"""
testdir = os.path.join(tmpdir, basename)
os.mkdir(testdir)
try:
os.chdir(testdir)
with _cwd(testdir):
return testfile(abspath, shell, indent=indent,
cleanenv=cleanenv, debug=debug,
testname=path)
finally:
os.chdir(cwd)

yield (path, test)
5 changes: 1 addition & 4 deletions cram/_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,8 +209,7 @@ def testfile(path, shell='/bin/sh', indent=2, env=None, cleanenv=True,
:return: Input, output, and diff iterables
:rtype: (list[bytes], list[bytes], collections.Iterable[bytes])
"""
f = open(path, 'rb')
try:
with open(path, 'rb') as f:
abspath = os.path.abspath(path)
env = env or os.environ.copy()
env['TESTDIR'] = os.fsdecode(os.path.dirname(abspath))
Expand All @@ -219,5 +218,3 @@ def testfile(path, shell='/bin/sh', indent=2, env=None, cleanenv=True,
testname = os.path.basename(abspath)
return test(f, shell, indent=indent, testname=testname, env=env,
cleanenv=cleanenv, debug=debug)
finally:
f.close()
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ check-manifest
coverage
pycodestyle
pyflakes
tox
28 changes: 28 additions & 0 deletions scripts/md5.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
""" Cli tool to calculate the md5 checksum for one or multiple files """
import sys
import argparse
from hashlib import md5


def _create_parser():
parser = argparse.ArgumentParser()
parser.add_argument(
'files',
metavar='FILE',
type=argparse.FileType('rb'),
nargs='+'
)
return parser


def main(argv=None):
parser = _create_parser()
args = parser.parse_args(argv)
for file in args.files:
hasher = md5()
hasher.update(file.read())
print(hasher.hexdigest())


if __name__ == '__main__':
sys.exit(main())
31 changes: 31 additions & 0 deletions tests/md5.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
Set up cram alias and example tests:

$ . "$TESTDIR"/setup.sh

Make sure md5 is aliased to our python implementation:

$ md5 -h | head -n 1
usage: md5.py.* (re)

Test md5 with a single input file:

$ cat > input-1.txt <<EOF
> FOO BAR
> LOREM IPSUM
> LOREM IPSUM LOREM IPSUM
> EOF

$ md5 input-1.txt
.*\b8832541c7c7fd2e8aed563c8e23a60f7\b.* (re)

Test md5 with multiple input files:

$ cat > input-2.txt <<EOF
> FOO BAR
> LOREM IPSUM LOREM IPSUM
> EOF

$ md5 input-1.txt input-2.txt input-1.txt
.*\b8832541c7c7fd2e8aed563c8e23a60f7\b.* (re)
.*\b06c8eaf05708ec6d207d582a3f9bcc59\b.* (re)
.*\b8832541c7c7fd2e8aed563c8e23a60f7\b.* (re)
2 changes: 1 addition & 1 deletion tests/pyflakes.t
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ Skip this test if pyflakes isn't available:

Check that there are no obvious Python source code errors:

$ pyflakes "$TESTDIR/.."
$ pyflakes "$TESTDIR/../cram"
6 changes: 4 additions & 2 deletions tests/setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

# Bash doesn't expand aliases by default in non-interactive mode, so
# we enable it manually if the test is run with --shell=/bin/bash.
[ "$TESTSHELL" = "/bin/bash" ] && shopt -s expand_aliases
[ -n "$BASH" ] && shopt -s expand_aliases

# The $PYTHON environment variable should be set when running this test
# from Python.
Expand All @@ -18,12 +18,14 @@ if [ -n "$COVERAGE" ]; then
$TESTDIR/../scripts/cram --shell=$TESTSHELL"
alias doctest="`which "$COVERAGE"` run -a --rcfile=$TESTDIR/../.coveragerc \
$TESTDIR/run-doctests.py"
alias md5="`which "$COVERAGE"` run -a --rcfile=$TESTDIR/../.coveragerc \
$TESTDIR/../scripts/md5.py"
else
PYTHON="`command -v "$PYTHON" || echo "$PYTHON"`"
alias cram="$PYTHON $TESTDIR/../scripts/cram --shell=$TESTSHELL"
alias doctest="$PYTHON $TESTDIR/run-doctests.py"
alias md5="$PYTHON $TESTDIR/../scripts/md5.py"
fi
command -v md5 > /dev/null || alias md5=md5sum

# Copy in example tests
cp -R "$TESTDIR"/../examples .
Expand Down
22 changes: 22 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
[tox]
#TODO: adjust set of used python interpreter(s)
envlist=py3-{integration}-{dash,bash,zsh},py3-{coverage}

[testenv]
setenv =
PYTHONPATH = {envpython}
PYTHONPATH = {toxinidir}
dash: TESTOPTS = --shell=dash
bash: TESTOPTS = --shell=bash
zsh: TESTOPTS = --shell=zsh
deps =
-r{toxinidir}/requirements.txt

commands =
integration: cram {env:TESTOPTS:--shell=dash} {toxinidir}/tests

[testenv:coverage]
setenv =
COVERAGE = coverage
commands =
coverage report --fail-under=100