From 4ab30c28c43bac1056835a07929fc12984c80cc5 Mon Sep 17 00:00:00 2001 From: Carson McManus Date: Wed, 5 Jan 2022 19:27:31 -0500 Subject: [PATCH] add a unit test for extracting a single text asset, mostly as an example closes #107 --- CONTRIBUTING.md | 9 +++++++++ unitypack/__init__.py | 6 +++--- unitypack/test/__init__.py | 0 unitypack/test/test_read_bundle.py | 26 ++++++++++++++++++++++++++ 4 files changed, 38 insertions(+), 3 deletions(-) create mode 100644 unitypack/test/__init__.py create mode 100644 unitypack/test/test_read_bundle.py diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 57b7f19..5b553fa 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -29,6 +29,15 @@ If you need to modify it or amend it in some way, you should always appropriatel Also see: [Github Help: Using Pull Requests](https://help.github.com/articles/using-pull-requests/) +### Running Tests + +To run unit tests, simply run: + +```bash +python3 -m unittest discover +``` + + ### Need help? You can always ask for help in our IRC channel, `#Hearthsim` on [Freenode](https://freenode.net/). diff --git a/unitypack/__init__.py b/unitypack/__init__.py index 78ea144..30a9024 100644 --- a/unitypack/__init__.py +++ b/unitypack/__init__.py @@ -1,7 +1,7 @@ -import pkg_resources - -__version__ = pkg_resources.require("unitypack")[0].version +if __name__ == '__main__': + import pkg_resources + __version__ = pkg_resources.require("unitypack")[0].version def load(file, env=None): diff --git a/unitypack/test/__init__.py b/unitypack/test/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/unitypack/test/test_read_bundle.py b/unitypack/test/test_read_bundle.py new file mode 100644 index 0000000..95b864c --- /dev/null +++ b/unitypack/test/test_read_bundle.py @@ -0,0 +1,26 @@ +from .. import * +import unittest +from .. import load +from pathlib import Path + +def get_bundle_fixture(name): + return Path(__file__).parent / 'fixtures' / 'bundles' / name + +class TestReadAssetBundle(unittest.TestCase): + def test_single_text_file(self): + with get_bundle_fixture("single-text-file-2019.3.13f1-no-compression").open("rb") as f: + bundle = load(f) + self.assertEqual(bundle.name, "CAB-2c069a3745be5cfe0c630ceac750b567") + self.assertFalse(bundle.compressed) + self.assertEqual(len(bundle.assets), 1) + asset = bundle.assets[0] + self.assertEqual(len(asset.objects), 2) + obj = list(asset.objects.items())[0][1] + self.assertEqual(obj.type, "TextAsset") + data = obj.read() + self.assertEqual(data.name, "example") + self.assertEqual(data.bytes, "ligma\n") + +if __name__ == '__main__': + unittest.main() +