blob: 6b63520d67bc1844d2eb64395458f0e6037323e3 [file] [log] [blame]
Adam Michael67d736b2016-10-06 21:31:57 +00001# Copyright 2016 The Bazel Authors. All rights reserved.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
Adam Michael78c19802016-10-13 23:09:25 +000015"""Tests for aar_embedded_jars_extractor."""
Adam Michael67d736b2016-10-06 21:31:57 +000016
Googler246ee562017-12-19 11:04:21 -080017import io
Adam Michael67d736b2016-10-06 21:31:57 +000018import os
Adam Michael97cc5be2016-10-25 21:25:20 +000019import shutil
Adam Michael67d736b2016-10-06 21:31:57 +000020import unittest
21import zipfile
22
Adam Michael78c19802016-10-13 23:09:25 +000023from tools.android import aar_embedded_jars_extractor
Adam Michael67d736b2016-10-06 21:31:57 +000024
25
Adam Michael97cc5be2016-10-25 21:25:20 +000026class AarEmbeddedJarsExtractor(unittest.TestCase):
Adam Michael78c19802016-10-13 23:09:25 +000027 """Unit tests for aar_embedded_jars_extractor.py."""
Adam Michael67d736b2016-10-06 21:31:57 +000028
Googler246ee562017-12-19 11:04:21 -080029 # Python 2 alias
30 if not hasattr(unittest.TestCase, "assertCountEqual"):
31
32 def assertCountEqual(self, *args):
33 return self.assertItemsEqual(*args)
34
Adam Michael97cc5be2016-10-25 21:25:20 +000035 def setUp(self):
36 os.chdir(os.environ["TEST_TMPDIR"])
Adam Michael67d736b2016-10-06 21:31:57 +000037
Adam Michael97cc5be2016-10-25 21:25:20 +000038 def tearDown(self):
39 shutil.rmtree("out_dir")
40
41 def testNoJars(self):
Googler246ee562017-12-19 11:04:21 -080042 aar = zipfile.ZipFile(io.BytesIO(), "w")
43 param_file = io.BytesIO()
Adam Michael216410b2016-11-08 15:42:25 +000044 os.makedirs("out_dir")
Adam Michael97cc5be2016-10-25 21:25:20 +000045 aar_embedded_jars_extractor.ExtractEmbeddedJars(aar, param_file, "out_dir")
46 self.assertEqual([], os.listdir("out_dir"))
47 param_file.seek(0)
Googler246ee562017-12-19 11:04:21 -080048 self.assertEqual(b"--exclude_build_data\n", param_file.read())
Adam Michael97cc5be2016-10-25 21:25:20 +000049
50 def testClassesJarAndLibsJars(self):
Googler246ee562017-12-19 11:04:21 -080051 aar = zipfile.ZipFile(io.BytesIO(), "w")
Adam Michael97cc5be2016-10-25 21:25:20 +000052 aar.writestr("classes.jar", "")
53 aar.writestr("libs/a.jar", "")
54 aar.writestr("libs/b.jar", "")
Googler246ee562017-12-19 11:04:21 -080055 param_file = io.BytesIO()
Adam Michael216410b2016-11-08 15:42:25 +000056 os.makedirs("out_dir")
Adam Michael97cc5be2016-10-25 21:25:20 +000057 aar_embedded_jars_extractor.ExtractEmbeddedJars(aar, param_file, "out_dir")
Googler246ee562017-12-19 11:04:21 -080058 self.assertCountEqual(["classes.jar", "libs"], os.listdir("out_dir"))
59 self.assertCountEqual(["a.jar", "b.jar"], os.listdir("out_dir/libs"))
Adam Michael97cc5be2016-10-25 21:25:20 +000060 param_file.seek(0)
61 self.assertEqual(
Googler246ee562017-12-19 11:04:21 -080062 [b"--exclude_build_data\n",
63 b"--sources\n",
64 b"out_dir/classes.jar\n",
65 b"--sources\n",
66 b"out_dir/libs/a.jar\n",
67 b"--sources\n",
68 b"out_dir/libs/b.jar\n"],
Adam Michael97cc5be2016-10-25 21:25:20 +000069 param_file.readlines())
70
71 def testOnlyClassesJar(self):
Googler246ee562017-12-19 11:04:21 -080072 aar = zipfile.ZipFile(io.BytesIO(), "w")
Adam Michael97cc5be2016-10-25 21:25:20 +000073 aar.writestr("classes.jar", "")
Googler246ee562017-12-19 11:04:21 -080074 param_file = io.BytesIO()
Adam Michael216410b2016-11-08 15:42:25 +000075 os.makedirs("out_dir")
Adam Michael97cc5be2016-10-25 21:25:20 +000076 aar_embedded_jars_extractor.ExtractEmbeddedJars(aar, param_file, "out_dir")
77 self.assertEqual(["classes.jar"], os.listdir("out_dir"))
78 param_file.seek(0)
79 self.assertEqual(
Googler246ee562017-12-19 11:04:21 -080080 [b"--exclude_build_data\n",
81 b"--sources\n",
82 b"out_dir/classes.jar\n"],
Adam Michael97cc5be2016-10-25 21:25:20 +000083 param_file.readlines())
Adam Michael67d736b2016-10-06 21:31:57 +000084
85
86if __name__ == "__main__":
87 unittest.main()