blob: 95ece58c68de9f68aec9016ef022dd36e96ad035 [file] [log] [blame]
Ned Nguyenbf081d02018-12-18 16:41:31 -07001# Copyright 2018 The Chromium OS Authors. All rights reserved.
2# 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):
18 """Tests for gconv_strip script."""
19
20 def testMultipleStringMatch(self):
Mike Frysinger2d589a12019-08-25 14:15:12 -040021 self.assertEqual(
Ned Nguyenbf081d02018-12-18 16:41:31 -070022 gconv_strip.MultipleStringMatch(
Mike Frysingerbdd40a12019-11-20 20:43:01 -050023 [b'hell', b'a', b'z', b'k', b'spec'],
24 b'hello_from a very special place'),
Ned Nguyenbf081d02018-12-18 16:41:31 -070025 [True, True, False, False, True])
26
27 def testModuleRewrite(self):
28 tmp_gconv_module = os.path.join(self.tempdir, 'gconv-modules')
29
30 data = """
31# from to module cost
32alias FOO charset_foo
33alias BAR charset_bar
34module charset_foo charset_bar UNUSED_MODULE
35
36# from to module cost
37alias CHAR_A charset_A
38alias EUROPE charset_B
39module charset_A charset_B USED_MODULE
40module charset_foo charset_A USED_MODULE
41"""
42 osutils.WriteFile(tmp_gconv_module, data)
43
44 gmods = gconv_strip.GconvModules(tmp_gconv_module)
Mike Frysingerbdd40a12019-11-20 20:43:01 -050045 self.assertEqual(gmods.Load(), [
46 'BAR', 'CHAR_A', 'EUROPE', 'FOO', 'charset_A', 'charset_B',
47 'charset_bar', 'charset_foo'])
Ned Nguyenbf081d02018-12-18 16:41:31 -070048 self.PatchObject(gconv_strip.lddtree, 'ParseELF', return_value={})
Mike Frysinger9c927782019-10-14 02:48:48 -040049 class _StubStat(object):
Mike Frysinger8e303f02020-02-14 22:53:11 -050050 """Fake for lstat."""
Mike Frysinger9c927782019-10-14 02:48:48 -040051 st_size = 0
52 self.PatchObject(gconv_strip.os, 'lstat', return_value=_StubStat)
Ned Nguyenbf081d02018-12-18 16:41:31 -070053 self.PatchObject(gconv_strip.os, 'unlink')
54 gmods.Rewrite(['charset_A', 'charset_B'], dry_run=False)
55
56 expected = """
57# from to module cost
58alias FOO charset_foo
59
60# from to module cost
61alias CHAR_A charset_A
62alias EUROPE charset_B
63module charset_A charset_B USED_MODULE
64module charset_foo charset_A USED_MODULE
65"""
66
67 content = osutils.ReadFile(tmp_gconv_module)
Mike Frysinger2d589a12019-08-25 14:15:12 -040068 self.assertEqual(content, expected)