blob: 4b1831bac6bf4d82d1b5ca361d53260491d92262 [file] [log] [blame]
Mike Frysingerf1ba7ad2022-09-12 05:42:57 -04001# Copyright 2018 The ChromiumOS Authors
Ned Nguyenbf081d02018-12-18 16:41:31 -07002# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5"""Test gconv_strip."""
6
Ned Nguyenbf081d02018-12-18 16:41:31 -07007import os
8
9from chromite.lib import cros_test_lib
10from chromite.lib import osutils
11from chromite.scripts import gconv_strip
12
Mike Frysinger807d8282022-04-28 22:45:17 -040013
Greg Edelstona4c9b3b2020-01-07 17:51:13 -070014pytestmark = cros_test_lib.pytestmark_inside_only
15
Ned Nguyenbf081d02018-12-18 16:41:31 -070016
17class GconvStriptTest(cros_test_lib.MockTempDirTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -060018 """Tests for gconv_strip script."""
Ned Nguyenbf081d02018-12-18 16:41:31 -070019
Alex Klein1699fab2022-09-08 08:46:06 -060020 def testMultipleStringMatch(self):
21 self.assertEqual(
22 gconv_strip.MultipleStringMatch(
23 [b"hell", b"a", b"z", b"k", b"spec"],
24 b"hello_from a very special place",
25 ),
26 [True, True, False, False, True],
27 )
Ned Nguyenbf081d02018-12-18 16:41:31 -070028
Alex Klein1699fab2022-09-08 08:46:06 -060029 def testModuleRewrite(self):
30 tmp_gconv_module = os.path.join(self.tempdir, "gconv-modules")
Ned Nguyenbf081d02018-12-18 16:41:31 -070031
Alex Klein1699fab2022-09-08 08:46:06 -060032 data = """
Ned Nguyenbf081d02018-12-18 16:41:31 -070033# from to module cost
34alias FOO charset_foo
35alias BAR charset_bar
36module charset_foo charset_bar UNUSED_MODULE
37
38# from to module cost
39alias CHAR_A charset_A
40alias EUROPE charset_B
41module charset_A charset_B USED_MODULE
42module charset_foo charset_A USED_MODULE
43"""
Alex Klein1699fab2022-09-08 08:46:06 -060044 osutils.WriteFile(tmp_gconv_module, data)
Ned Nguyenbf081d02018-12-18 16:41:31 -070045
Alex Klein1699fab2022-09-08 08:46:06 -060046 gmods = gconv_strip.GconvModules(tmp_gconv_module)
47 self.assertEqual(
48 gmods.Load(),
49 [
50 "BAR",
51 "CHAR_A",
52 "EUROPE",
53 "FOO",
54 "charset_A",
55 "charset_B",
56 "charset_bar",
57 "charset_foo",
58 ],
59 )
60 self.PatchObject(gconv_strip.lddtree, "ParseELF", return_value={})
Ned Nguyenbf081d02018-12-18 16:41:31 -070061
Alex Klein1699fab2022-09-08 08:46:06 -060062 class _StubStat(object):
63 """Fake for lstat."""
64
65 st_size = 0
66
67 self.PatchObject(gconv_strip.os, "lstat", return_value=_StubStat)
68 self.PatchObject(gconv_strip.os, "unlink")
Mike Frysinger61b792c2023-02-02 09:02:27 -050069 gmods.Rewrite(["charset_A", "charset_B"], dryrun=False)
Alex Klein1699fab2022-09-08 08:46:06 -060070
71 expected = """
Ned Nguyenbf081d02018-12-18 16:41:31 -070072# from to module cost
73alias FOO charset_foo
74
75# from to module cost
76alias CHAR_A charset_A
77alias EUROPE charset_B
78module charset_A charset_B USED_MODULE
79module charset_foo charset_A USED_MODULE
80"""
81
Alex Klein1699fab2022-09-08 08:46:06 -060082 content = osutils.ReadFile(tmp_gconv_module)
83 self.assertEqual(content, expected)