Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / native_client / build / package_version / revision_info_test.py
1 #!/usr/bin/python
2 # Copyright (c) 2014 The Native Client Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file.
5
6 """Tests for packages info."""
7
8 import json
9 import os
10 import sys
11 import unittest
12
13 sys.path.append(os.path.join(os.path.dirname(__file__), '../..'))
14 import pynacl.working_directory
15
16 import archive_info
17 import error
18 import package_info
19 import packages_info
20 import revision_info
21
22 CURRENT_DIR = os.path.dirname(os.path.abspath(__file__))
23 TEST_PACKAGES_JSON = os.path.join(CURRENT_DIR, 'test_packages.json')
24 TEST_PLATFORM = 'platform'
25 TEST_ARCH_ALL = 'arch_all'
26 TEST_ARCH_SHARED = 'arch_shared'
27 TEST_ARCH_NON_SHARED = 'arch_non_shared'
28 TEST_EMPTY_PACKAGE_TARGET = 'empty_package_target'
29 TEST_SINGLE_PACKAGE_PACKAGE_TARGET = 'package_1'
30 TEST_MULTI_PACKAGE_PACKAGE_TARGET = 'package_2'
31
32
33 class TestRevisionInfo(unittest.TestCase):
34
35   def setUp(self):
36     self._packages = packages_info.PackagesInfo(TEST_PACKAGES_JSON)
37
38   def test_RevTargetSets(self):
39     # Tests that we can properly set a target revision.
40     package = package_info.PackageInfo()
41     package.AppendArchive(archive_info.ArchiveInfo(name='test_name',
42                                                    hash='hash_value'))
43
44     revision_desc = revision_info.RevisionInfo(self._packages)
45     revision_desc.SetTargetRevision('test_package', 'package_target', package)
46
47     self.assertEqual(package, revision_desc.GetPackageInfo('package_target'))
48
49   def test_RevisionTargetSamePackage(self):
50     # Tests that all the targets must all be the same.
51     package = package_info.PackageInfo()
52     package.AppendArchive(archive_info.ArchiveInfo(name='test_name',
53                                                    hash='hash_value'))
54
55     revision_desc = revision_info.RevisionInfo(self._packages)
56     revision_desc.SetTargetRevision('test1', 'package_target', package)
57
58     self.assertRaises(
59         error.Error,
60         revision_desc.SetTargetRevision,
61         'test2',
62         'package_target',
63         package
64     )
65
66   def test_RevisionFileSaveLoad(self):
67     # Tests that we can properly save and load a revision file.
68     package = package_info.PackageInfo()
69     package.AppendArchive(archive_info.ArchiveInfo(name='test_name',
70                                                    hash='hash_value'))
71
72     revision = revision_info.RevisionInfo(self._packages)
73     revision.SetRevisionNumber(100)
74     package_targets = self._packages.GetPackageTargetsForPackage(
75         TEST_SINGLE_PACKAGE_PACKAGE_TARGET
76     )
77     self.assertEqual(
78         1,
79         len(package_targets),
80         "Invalid test data, single package package target requires 1 target"
81     )
82
83     revision.SetTargetRevision(
84         TEST_SINGLE_PACKAGE_PACKAGE_TARGET,
85         package_targets[0],
86         package
87     )
88
89     with pynacl.working_directory.TemporaryWorkingDirectory() as work_dir:
90       revision_file = os.path.join(work_dir, 'test_revision.json')
91       revision.SaveRevisionFile(revision_file)
92
93       new_revision = revision_info.RevisionInfo(self._packages, revision_file)
94
95     self.assertEqual(revision, new_revision)
96
97   def test_RevisionFileRequiresRevisionNumber(self):
98     # Tests that we can properly save and load a revision file.
99     package = package_info.PackageInfo()
100     package.AppendArchive(archive_info.ArchiveInfo(name='test_name',
101                                                    hash='hash_value'))
102
103     revision = revision_info.RevisionInfo(self._packages)
104     package_targets = self._packages.GetPackageTargetsForPackage(
105         TEST_SINGLE_PACKAGE_PACKAGE_TARGET
106     )
107     for package_target in package_targets:
108       revision.SetTargetRevision(
109           TEST_SINGLE_PACKAGE_PACKAGE_TARGET,
110           package_target,
111           package
112       )
113
114     with pynacl.working_directory.TemporaryWorkingDirectory() as work_dir:
115       revision_file = os.path.join(work_dir, 'test_revision.json')
116
117       self.assertRaises(
118           error.Error,
119           revision.SaveRevisionFile,
120           revision_file
121       )
122
123   def test_AlteredRevisionFileFails(self):
124     # Tests that an altered revision file will fail to load.
125     package = package_info.PackageInfo()
126     package.AppendArchive(archive_info.ArchiveInfo(name='test_name',
127                                                    hash='hash_value'))
128
129     revision = revision_info.RevisionInfo(self._packages)
130     revision.SetRevisionNumber(100)
131     package_targets = self._packages.GetPackageTargetsForPackage(
132         TEST_SINGLE_PACKAGE_PACKAGE_TARGET
133     )
134     for package_target in package_targets:
135       revision.SetTargetRevision(
136           TEST_SINGLE_PACKAGE_PACKAGE_TARGET,
137           package_target,
138           package
139       )
140
141     with pynacl.working_directory.TemporaryWorkingDirectory() as work_dir:
142       revision_file = os.path.join(work_dir, 'altered_revision.json')
143       revision.SaveRevisionFile(revision_file)
144
145       # Alter the file directly and save it back out
146       with open(revision_file, 'rt') as f:
147         revision_json = json.load(f)
148       revision_json[revision_info.FIELD_REVISION] = 'noise'
149       with open(revision_file, 'wt') as f:
150         json.dump(revision_json, f)
151
152       new_revision = revision_info.RevisionInfo(self._packages)
153       self.assertRaises(
154           error.Error,
155           new_revision.LoadRevisionFile,
156           revision_file
157       )
158
159   def test_RevisionFileMustSetAllTargets(self):
160     # Tests that a revision file fails if not all package targets are set.
161     package = package_info.PackageInfo()
162     package.AppendArchive(archive_info.ArchiveInfo(name='test_name',
163                                                    hash='hash_value'))
164
165     package_targets = self._packages.GetPackageTargetsForPackage(
166         TEST_MULTI_PACKAGE_PACKAGE_TARGET
167     )
168     self.assertTrue(
169         len(package_targets) > 0,
170         'Invalid test data, multiple package targets expected'
171     )
172
173     revision = revision_info.RevisionInfo(self._packages)
174     revision.SetRevisionNumber(100)
175     revision.SetTargetRevision(
176         TEST_MULTI_PACKAGE_PACKAGE_TARGET,
177         package_targets[0],
178         package
179     )
180
181     with pynacl.working_directory.TemporaryWorkingDirectory() as work_dir:
182       revision_file = os.path.join(work_dir, 'incomplete_revision.json')
183       self.assertRaises(
184           error.Error,
185           revision.SaveRevisionFile,
186           revision_file
187       )
188
189   def test_RevisionFileSavesForMultiTargets(self):
190     # Tests that a revision successfully saves a multi-package target package.
191     package = package_info.PackageInfo()
192     package.AppendArchive(archive_info.ArchiveInfo(name='test_name',
193                                                    hash='hash_value'))
194
195     package_targets = self._packages.GetPackageTargetsForPackage(
196         TEST_MULTI_PACKAGE_PACKAGE_TARGET
197     )
198     self.assertTrue(
199         len(package_targets) > 0,
200         'Invalid test data, multiple package targets expected'
201     )
202
203     revision = revision_info.RevisionInfo(self._packages)
204     revision.SetRevisionNumber(100)
205     for package_target in package_targets:
206       revision.SetTargetRevision(
207           TEST_MULTI_PACKAGE_PACKAGE_TARGET,
208           package_target,
209           package
210       )
211
212     with pynacl.working_directory.TemporaryWorkingDirectory() as work_dir:
213       revision_file = os.path.join(work_dir, 'complete_revision.json')
214       revision.SaveRevisionFile(revision_file)
215
216       new_revision = revision_info.RevisionInfo(self._packages, revision_file)
217
218     self.assertEqual(revision, new_revision)
219
220
221 if __name__ == '__main__':
222   unittest.main()