blob: fcc70e1569d9029386ab13838224d8348beb28da [file] [log] [blame]
Ned Nguyenbf081d02018-12-18 16:41:31 -07001# -*- coding: utf-8 -*-
2# Copyright 2018 The Chromium OS 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"""Test gconv_strip."""
7
8from __future__ import print_function
9
10import os
11
12from chromite.lib import cros_test_lib
13from chromite.lib import osutils
14from chromite.scripts import gconv_strip
15
16
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):
50 st_size = 0
51 self.PatchObject(gconv_strip.os, 'lstat', return_value=_StubStat)
Ned Nguyenbf081d02018-12-18 16:41:31 -070052 self.PatchObject(gconv_strip.os, 'unlink')
53 gmods.Rewrite(['charset_A', 'charset_B'], dry_run=False)
54
55 expected = """
56# from to module cost
57alias FOO charset_foo
58
59# from to module cost
60alias CHAR_A charset_A
61alias EUROPE charset_B
62module charset_A charset_B USED_MODULE
63module charset_foo charset_A USED_MODULE
64"""
65
66 content = osutils.ReadFile(tmp_gconv_module)
Mike Frysinger2d589a12019-08-25 14:15:12 -040067 self.assertEqual(content, expected)