提交 2a07cfb4 编写于 作者: HansBug's avatar HansBug 😆

Merge branch 'main' into doc/readme

......@@ -58,7 +58,7 @@ jobs:
matrix:
os:
- 'ubuntu-18.04'
# - 'windows-2019'
- 'windows-2019'
- 'macos-10.15'
python:
- '3.6'
......
......@@ -49,7 +49,7 @@ jobs:
matrix:
os:
- 'ubuntu-18.04'
# - 'windows-2019'
- 'windows-2019'
- 'macos-10.15'
python:
- '3.6'
......
......@@ -18,7 +18,7 @@ jobs:
matrix:
os:
- 'ubuntu-18.04'
# - 'windows-2019' # need to be fixed, see: https://github.com/opendilab/treevalue/issues/41
- 'windows-2019' # need to be fixed, see: https://github.com/opendilab/treevalue/issues/41
- 'macos-10.15'
python-version:
- '3.6'
......@@ -29,21 +29,21 @@ jobs:
steps:
- name: Get system version for Linux
if: ${{ contains(matrix.os, 'ubuntu') }}
if: ${{ runner.os == 'Linux' }}
shell: bash
run: |
echo "OS_NAME=Linux" >> $GITHUB_ENV
echo "IS_WIN=" >> $GITHUB_ENV
echo "IS_MAC=" >> $GITHUB_ENV
- name: Get system version for Windows
if: ${{ contains(matrix.os, 'windows') }}
if: ${{ runner.os == 'Windows' }}
shell: bash
run: |
echo "OS_NAME=Windows" >> $GITHUB_ENV
echo "IS_WIN=1" >> $GITHUB_ENV
echo "IS_MAC=" >> $GITHUB_ENV
- name: Get system version for MacOS
if: ${{ contains(matrix.os, 'macos') }}
if: ${{ runner.os == 'macOS' }}
shell: bash
run: |
echo "OS_NAME=MacOS" >> $GITHUB_ENV
......
......@@ -8,11 +8,23 @@ requires = [
[tool.cibuildwheel]
skip = ["pp*"] # Do not build for PyPy
## Windows build configuration
[tool.cibuildwheel.windows]
archs = ["x86", 'AMD64']
before-test = [# Unittest for windows
"pip install -r \"{project}\\requirements-test-win.txt\"",
]
test-command = [
"xcopy /e /i \"{project}\\test\" test",
"copy \"{project}\\pytest.ini\" pytest.ini",
"pytest test -sv -m unittest --log-level=DEBUG",
"rmdir /s /q test",
]
## macOS build configuration
[tool.cibuildwheel.macos]
archs = ["x86_64", "arm64"] # Build for x86_64 and arm64
before-test = [# Unittest for linux
before-test = [# Unittest for macos
"pip install -r {project}/requirements-test.txt",
]
test-command = [
......
coverage>=5
mock>=4.0.3
flake8~=3.5
pytest~=6.2.5
pytest-cov~=3.0.0
pytest-mock~=3.6.1
pytest-xdist>=1.34.0
pytest-rerunfailures~=10.2
pytest-timeout~=2.0.2
pytest-benchmark~=3.4.0
testtools>=2
hbutils>=0.6.13
setuptools<=59.5.0
numpy>=1.10
easydict>=1.7,<2
\ No newline at end of file
......@@ -30,7 +30,8 @@ class TestEntryCliExport:
with runner.isolated_filesystem():
result = runner.invoke(treevalue_cli, ['export', '-t', 'test.entry.cli.test_export.t1'])
assert result.exit_code == 0
assert result.exit_code == 0, f'Runtime Error (exitcode {result.exit_code}), ' \
f'The output is:\n{result.output}'
assert os.path.exists('t1.btv')
assert os.path.getsize('t1.btv') < 240
......@@ -42,7 +43,8 @@ class TestEntryCliExport:
with runner.isolated_filesystem():
result = runner.invoke(treevalue_cli, ['export', '-t', 'test.entry.cli.test_export.*'])
assert result.exit_code == 0
assert result.exit_code == 0, f'Runtime Error (exitcode {result.exit_code}), ' \
f'The output is:\n{result.output}'
assert os.path.exists('t1.btv')
assert os.path.getsize('t1.btv') < 240
assert os.path.exists('t2.btv')
......@@ -60,7 +62,8 @@ class TestEntryCliExport:
with runner.isolated_filesystem():
result = runner.invoke(treevalue_cli, ['export', '-t', 'test.entry.cli.test_export.t[23]'])
assert result.exit_code == 0
assert result.exit_code == 0, f'Runtime Error (exitcode {result.exit_code}), ' \
f'The output is:\n{result.output}'
assert not os.path.exists('t1.btv')
assert os.path.exists('t2.btv')
assert os.path.getsize('t2.btv') < 290
......@@ -78,7 +81,8 @@ class TestEntryCliExport:
with pytest.warns(None):
result = runner.invoke(treevalue_cli, ['export', '-t', 'test.entry.cli.test_export.*', '-r'])
assert result.exit_code == 0
assert result.exit_code == 0, f'Runtime Error (exitcode {result.exit_code}), ' \
f'The output is:\n{result.output}'
assert os.path.exists('t1.btv')
assert os.path.getsize('t1.btv') < 2170
assert os.path.exists('t2.btv')
......@@ -98,7 +102,8 @@ class TestEntryCliExport:
result = runner.invoke(treevalue_cli, ['export', '-t', 'test.entry.cli.test_export.*',
'-r', '-c', 'zlib'])
assert result.exit_code == 0
assert result.exit_code == 0, f'Runtime Error (exitcode {result.exit_code}), ' \
f'The output is:\n{result.output}'
assert os.path.exists('t1.btv')
assert os.path.getsize('t1.btv') < 2170
assert os.path.exists('t2.btv')
......@@ -119,7 +124,8 @@ class TestEntryCliExport:
result = runner.invoke(treevalue_cli, ['export', '-t', 'test.entry.cli.test_export.*',
'-c', 'gzip'])
assert result.exit_code == 0
assert result.exit_code == 0, f'Runtime Error (exitcode {result.exit_code}), ' \
f'The output is:\n{result.output}'
assert os.path.exists('t1.btv')
assert os.path.getsize('t1.btv') < 220
assert os.path.exists('t2.btv')
......@@ -140,7 +146,8 @@ class TestEntryCliExport:
'-c', 'test.entry.cli.test_export._my_compress:test.entry.cli.test_export._my_decompress'
])
assert result.exit_code == 0
assert result.exit_code == 0, f'Runtime Error (exitcode {result.exit_code}), ' \
f'The output is:\n{result.output}'
assert os.path.exists('t1.btv')
assert os.path.getsize('t1.btv') < 330
assert os.path.exists('t2.btv')
......@@ -167,7 +174,8 @@ class TestEntryCliExport:
'-o', 'subpath/t3.btv',
])
assert result.exit_code == 0
assert result.exit_code == 0, f'Runtime Error (exitcode {result.exit_code}), ' \
f'The output is:\n{result.output}'
assert os.path.exists('subpath/t1.btv')
assert os.path.getsize('subpath/t1.btv') < 290
assert os.path.exists('subpath/t2.btv')
......@@ -190,7 +198,8 @@ class TestEntryCliExport:
'-d', 'subpath',
])
assert result.exit_code == 0
assert result.exit_code == 0, f'Runtime Error (exitcode {result.exit_code}), ' \
f'The output is:\n{result.output}'
assert os.path.exists('subpath/t1.btv')
assert os.path.getsize('subpath/t1.btv') < 290
assert os.path.exists('subpath/t2.btv')
......@@ -210,7 +219,8 @@ class TestEntryCliExport:
'-d', 'subpath',
])
assert result.exit_code == 0
assert result.exit_code == 0, f'Runtime Error (exitcode {result.exit_code}), ' \
f'The output is:\n{result.output}'
assert not os.path.exists('subpath/t1.btv')
assert os.path.exists('subpath/t2.btv')
assert os.path.getsize('subpath/t2.btv') < 290
......@@ -229,7 +239,8 @@ class TestEntryCliExport:
'-o', 'ppp.btv'
])
assert result.exit_code == 0
assert result.exit_code == 0, f'Runtime Error (exitcode {result.exit_code}), ' \
f'The output is:\n{result.output}'
assert not os.path.exists('t1.btv')
assert not os.path.exists('t2.btv')
assert os.path.exists('ppp.btv')
......
......@@ -7,7 +7,7 @@ import zlib
import pytest
from click.testing import CliRunner
from hbutils.testing import cmdv
from hbutils.testing import cmdv, OS
from treevalue import FastTreeValue, dump, graphics
from treevalue.entry.cli import treevalue_cli
......@@ -36,7 +36,8 @@ class TestEntryCliGraph:
'-o', 'test_graph.svg', '-o', 'test_graph.gv'],
)
assert result.exit_code == 0
assert result.exit_code == 0, f'Runtime Error (exitcode {result.exit_code}), ' \
f'The output is:\n{result.output}'
assert os.path.exists('test_graph.svg')
assert os.path.getsize('test_graph.svg') <= 7000
assert os.path.exists('test_graph.gv')
......@@ -52,7 +53,8 @@ class TestEntryCliGraph:
'-o', 'test_graph.svg', '-o', 'test_graph.gv', '-O'],
)
assert result.exit_code == 0
assert result.exit_code == 0, f'Runtime Error (exitcode {result.exit_code}), ' \
f'The output is:\n{result.output}'
assert not os.path.exists('test_graph.svg')
assert not os.path.exists('test_graph.gv')
assert len(result.output) <= 2500
......@@ -66,7 +68,8 @@ class TestEntryCliGraph:
args=['graph', '-t', 'test.entry.cli.test_graph.t[12]', '-o', 'test_graph.svg'],
)
assert result.exit_code == 0
assert result.exit_code == 0, f'Runtime Error (exitcode {result.exit_code}), ' \
f'The output is:\n{result.output}'
assert os.path.exists('test_graph.svg')
assert os.path.getsize('test_graph.svg') <= 13000
......@@ -76,7 +79,8 @@ class TestEntryCliGraph:
args=['graph', '-t', 'test.entry.cli.test_graph.*', '-o', 'test_graph.svg'],
)
assert result.exit_code == 0
assert result.exit_code == 0, f'Runtime Error (exitcode {result.exit_code}), ' \
f'The output is:\n{result.output}'
assert os.path.exists('test_graph.svg')
assert os.path.getsize('test_graph.svg') <= 17500
......@@ -92,7 +96,8 @@ class TestEntryCliGraph:
args=['graph', '-t', 'g1.bg', '-o', 'test_graph.svg'],
)
assert result.exit_code == 0
assert result.exit_code == 0, f'Runtime Error (exitcode {result.exit_code}), ' \
f'The output is:\n{result.output}'
assert os.path.exists('test_graph.svg')
assert os.path.getsize('test_graph.svg') <= 6500
......@@ -109,7 +114,8 @@ class TestEntryCliGraph:
args=['graph', '-t', '*.bg', '-o', 'test_graph.svg'],
)
assert result.exit_code == 0
assert result.exit_code == 0, f'Runtime Error (exitcode {result.exit_code}), ' \
f'The output is:\n{result.output}'
assert os.path.exists('test_graph.svg')
assert os.path.getsize('test_graph.svg') <= 17500
......@@ -122,7 +128,8 @@ class TestEntryCliGraph:
args=['graph', '-t', 'test.entry.cli.test_graph.t1', '-o', 'test_graph.svg'],
)
assert result.exit_code == 0
assert result.exit_code == 0, f'Runtime Error (exitcode {result.exit_code}), ' \
f'The output is:\n{result.output}'
assert os.path.exists('test_graph.svg')
assert os.path.getsize('test_graph.svg') <= 13000
......@@ -135,7 +142,8 @@ class TestEntryCliGraph:
args=['graph', '-t', 'test.entry.cli.test_graph.t1', '-o', 'test_graph.svg'],
)
assert result.exit_code == 0
assert result.exit_code == 0, f'Runtime Error (exitcode {result.exit_code}), ' \
f'The output is:\n{result.output}'
assert os.path.exists('test_graph.svg')
assert os.path.getsize('test_graph.svg') <= 6500
......@@ -149,7 +157,8 @@ class TestEntryCliGraph:
'-o', 'test_graph.svg'],
)
assert result.exit_code == 0
assert result.exit_code == 0, f'Runtime Error (exitcode {result.exit_code}), ' \
f'The output is:\n{result.output}'
assert os.path.exists('test_graph.svg')
assert os.path.getsize('test_graph.svg') <= 12000
......@@ -162,7 +171,8 @@ class TestEntryCliGraph:
'-o', 'test_graph.svg'],
)
assert result.exit_code == 0
assert result.exit_code == 0, f'Runtime Error (exitcode {result.exit_code}), ' \
f'The output is:\n{result.output}'
assert os.path.exists('test_graph.svg')
import shutil
shutil.copy('test_graph.svg', os.path.join(_p, 'test_graph.svg'))
......@@ -179,7 +189,8 @@ class TestEntryCliGraph:
'-t', 'first title', '-o', 'test_graph.svg'],
)
assert result.exit_code == 0
assert result.exit_code == 0, f'Runtime Error (exitcode {result.exit_code}), ' \
f'The output is:\n{result.output}'
assert os.path.exists('test_graph.svg')
assert os.path.getsize('test_graph.svg') <= 16500
......@@ -196,7 +207,8 @@ class TestEntryCliGraph:
'-c', 'bgcolor=#ffffff00', '-O'],
)
assert result.exit_code == 0
assert result.exit_code == 0, f'Runtime Error (exitcode {result.exit_code}), ' \
f'The output is:\n{result.output}'
assert len(result.output) <= 6000
assert '#ffffff00' in result.output
......@@ -207,7 +219,9 @@ class TestEntryCliGraph:
'-c', 'bgcolor#ffffff00', '-O'],
)
assert result.exit_code != 0
assert result.exit_code != 0, f'The running expected to raise RuntimeError ' \
f'but not actually (exitcode {result.exit_code}), ' \
f'The output is:\n{result.output}'
assert "Configuration should be KEY=VALUE, but 'bgcolor#ffffff00' found." in result.output
@unittest.skipUnless(cmdv('dot'), 'Dot installed only')
......@@ -227,6 +241,10 @@ class TestEntryCliGraph:
args=['graph', '-t', 'g1.bg', '-o', 'test_graph.svg'],
)
assert result.exit_code == 0
assert result.exit_code == 0, f'Runtime Error (exitcode {result.exit_code}), ' \
f'The output is:\n{result.output}'
assert os.path.exists('test_graph.svg')
assert os.path.getsize('test_graph.svg') <= 1000
if OS.windows:
assert os.path.getsize('test_graph.svg') <= 2000
else:
assert os.path.getsize('test_graph.svg') <= 1000
......@@ -11,6 +11,7 @@ class TestEntryCliVersion:
runner = CliRunner()
result = runner.invoke(treevalue_cli, args=['-v'])
assert result.exit_code == 0
assert result.exit_code == 0, f'Runtime Error (exitcode {result.exit_code}), ' \
f'The output is:\n{result.output}'
assert __TITLE__.lower() in result.stdout.lower()
assert __VERSION__.lower() in result.stdout.lower()
import unittest
from functools import lru_cache
from typing import Optional
from hbutils.testing import vpip
try:
import torch
except ImportError:
torch = None
import pytest
import torch
from treevalue import TreeValue, func_treelize, FastTreeValue
_TREE_DATA_1 = {'a': torch.randn(2, 3), 'x': {'c': torch.randn(3, 4)}}
_TREE_1 = FastTreeValue(_TREE_DATA_1)
@lru_cache()
def _get_tree() -> Optional[FastTreeValue]:
if torch is not None:
_TREE_DATA_1 = {'a': torch.randn(2, 3), 'x': {'c': torch.randn(3, 4)}}
return FastTreeValue(_TREE_DATA_1)
else:
return None
@pytest.mark.benchmark(group='treevalue_dynamic')
@unittest.skipUnless(vpip('torch') >= '1.1.0', 'Torch>=1.1.0 only')
class TestTreeGeneralBenchmark:
def test_dynamic_execute(self, benchmark):
def sin(t):
return t.sin()
return benchmark(sin, _TREE_1)
return benchmark(sin, _get_tree())
def test_static_execute(self, benchmark):
sinf = func_treelize(return_type=TreeValue)(torch.sin)
......@@ -21,4 +38,4 @@ class TestTreeGeneralBenchmark:
def sin(t):
return sinf(t)
return benchmark(sin, _TREE_1)
return benchmark(sin, _get_tree())
import unittest
import pytest
import torch
from hbutils.testing import vpip
try:
import torch
except ImportError:
torch = None
from treevalue.tree import func_treelize, FastTreeValue
@pytest.mark.unittest
@unittest.skipUnless(vpip('torch') >= '1.1.0', 'Torch>=1.1.0 only')
def test_for_torch_support():
sin = func_treelize()(torch.sin)
cos = func_treelize()(torch.cos) # the same sin function
......
......@@ -19,6 +19,8 @@
# And in this library 'treevalue', Apache Licence Version 2.0 is used as well.
import doctest
import io
import os
from operator import itemgetter
from textwrap import dedent
......@@ -31,6 +33,11 @@ from treevalue.utils import (
)
def _process_linesep(s: str) -> str:
with io.StringIO(s) as sf:
return os.linesep.join([line.rstrip('\r\n') for line in sf])
@pytest.mark.unittest
class TestFormatTree(TestCase):
......@@ -40,9 +47,9 @@ class TestFormatTree(TestCase):
def test_single_node_tree(self):
tree = ('foo', [])
output = self.format_tree(tree)
self.assertEqual(dedent(u'''\
self.assertEqual(_process_linesep(dedent(u'''\
foo
'''), output)
''')), _process_linesep(output))
def test_single_level_tree(self):
tree = (
......@@ -53,12 +60,12 @@ class TestFormatTree(TestCase):
],
)
output = self.format_tree(tree)
self.assertEqual(dedent(u'''\
self.assertEqual(_process_linesep(dedent(u'''\
foo
├── bar
├── baz
└── qux
'''), output)
''')), _process_linesep(output))
def test_single_level_tree_with_ascii(self):
tree = (
......@@ -69,12 +76,12 @@ class TestFormatTree(TestCase):
],
)
output = self.format_tree(tree, encoding='ascii')
self.assertEqual(dedent(u'''\
self.assertEqual(_process_linesep(dedent(u'''\
foo
+-- bar
+-- baz
`-- qux
'''), output)
''')), _process_linesep(output))
def test_multi_level_tree(self):
tree = (
......@@ -88,14 +95,14 @@ class TestFormatTree(TestCase):
],
)
output = self.format_tree(tree)
self.assertEqual(dedent(u'''\
self.assertEqual(_process_linesep(dedent(u'''\
foo
├── bar
│ ├── a
│ └── b
├── baz
└── qux
'''), output)
''')), _process_linesep(output))
def test_multi_level_on_last_node_tree(self):
tree = (
......@@ -109,14 +116,14 @@ class TestFormatTree(TestCase):
],
)
output = self.format_tree(tree)
self.assertEqual(dedent(u'''\
self.assertEqual(_process_linesep(dedent(u'''\
foo
├── bar
├── baz
└── qux
├── a
└── b
'''), output)
''')), _process_linesep(output))
def test_acceptance(self):
output = self.format_tree(ACCEPTANCE_INPUT)
......@@ -140,7 +147,7 @@ class TestFormatTree(TestCase):
],
)
output = self.format_tree(tree)
self.assertEqual(dedent(u'''\
self.assertEqual(_process_linesep(dedent(u'''\
foo
├── bar
│ frob
......@@ -151,7 +158,7 @@ class TestFormatTree(TestCase):
├── baz
└── qux
frab
'''), output)
''')), _process_linesep(output))
def d(name, files):
......
import codecs
import os
import shutil
import tempfile
import warnings
from collections import OrderedDict
from functools import partial
......@@ -61,10 +59,7 @@ def _save_source_code(g: Digraph, path: str):
def _save_image(g: Digraph, path: str, fmt: str):
with tempfile.TemporaryDirectory() as tmpdir:
with tempfile.NamedTemporaryFile(dir=tmpdir) as tmpfile:
svg_file = g.render(tmpfile.name, format=fmt)
shutil.copy(svg_file, path)
g.render(path, format=fmt)
_IMAGE_FMTS = {'svg', 'png'}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册