File size: 3,341 Bytes
a65138c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# -*- coding: utf-8 -*-
from __future__ import with_statement
import unittest
from oxt_tool.package import is_package
from oxt_tool.storage import open_storage
from oxt_tool.storage import resolve_path
from oxt_tool.storage.fs import FileSystemStorage
from oxt_tool.manifest import Manifest
from oxt_tool.description import Description


class PackageTest(unittest.TestCase):

    def test_package_name_from_desc(self):
        from oxt_tool.package import package_name_from_desc
        desc = Description(identifier='pyhwp.example', version='')
        self.assertEqual('pyhwp.example.oxt', package_name_from_desc(desc))
        desc.version = '0.1'
        self.assertEqual('pyhwp.example-0.1.oxt', package_name_from_desc(desc))

    def test_make_output_path(self):
        from oxt_tool.package import make_output_path

        self.assertEqual('abc.oxt', make_output_path('abc.oxt'))
        self.assertEqual('./abc.oxt', make_output_path('./abc.oxt'))
        self.assertEqual('abc/def.oxt', make_output_path('abc/def.oxt'))

        desc = Description(identifier='example', version='0.1')
        self.assertEqual('example-0.1.oxt', make_output_path('', desc))
        self.assertEqual('./example-0.1.oxt', make_output_path('.', desc))
        self.assertEqual('abc/example-0.1.oxt', make_output_path('abc/', desc))

        dirpath = self.id()
        import shutil
        import os.path
        if os.path.exists(dirpath):
            shutil.rmtree(dirpath)
        os.mkdir(dirpath)
        self.assertEqual(os.path.join(dirpath, 'example-0.1.oxt'),
                          make_output_path(dirpath, desc))


class BuildPackageTest(unittest.TestCase):

    def test_build_minimal(self):
        from oxt_tool.package import build

        manifest = Manifest()
        description = Description()
        oxt_path = self.id() + '.oxt'
        build(oxt_path, manifest, description)
        with open_storage(oxt_path) as pkg:
            self.assertTrue(is_package(pkg))

    def test_build_missing(self):
        from oxt_tool.package import build

        oxt_path = self.id() + '.oxt'

        manifest = Manifest()
        description = Description(license=dict(en='COPYING'))
        files = dict()
        try:
            build(oxt_path, manifest, description, files=files)
            assert False, 'exception expected'
        except Exception:
            pass

    def test_build_typical(self):
        from oxt_tool.package import build
        from oxt_tool.storage import makedirs_to_file

        manifest = Manifest()
        description = Description()

        import os.path
        import shutil
        src_folder_path = self.id()
        if os.path.exists(src_folder_path):
            shutil.rmtree(src_folder_path)
        src_folder = FileSystemStorage(src_folder_path, 'w')

        license_path = 'COPYING'
        license_node = makedirs_to_file(src_folder, license_path)
        with license_node.open('w') as f:
            f.write('GNU AGPL')
        description.license['en'] = license_path

        oxt_path = self.id() + '.oxt'

        files = {license_path: license_node}
        build(oxt_path, manifest, description, files=files)

        with open_storage(oxt_path) as pkg:
            with resolve_path(pkg, 'COPYING').open() as f:
                self.assertEqual('GNU AGPL', f.read())