blob: db1900342f006163cd745aae417314c631ad4243 [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
Mike Frysinger8e303f02020-02-14 22:53:11 -050011import sys
Ned Nguyenbf081d02018-12-18 16:41:31 -070012
13from chromite.lib import cros_test_lib
14from chromite.lib import osutils
15from chromite.scripts import gconv_strip
16
17
Mike Frysinger8e303f02020-02-14 22:53:11 -050018assert sys.version_info >= (3, 6), 'This module requires Python 3.6+'
19
20
Ned Nguyenbf081d02018-12-18 16:41:31 -070021class GconvStriptTest(cros_test_lib.MockTempDirTestCase):
22 """Tests for gconv_strip script."""
23
24 def testMultipleStringMatch(self):
Mike Frysinger2d589a12019-08-25 14:15:12 -040025 self.assertEqual(
Ned Nguyenbf081d02018-12-18 16:41:31 -070026 gconv_strip.MultipleStringMatch(
Mike Frysingerbdd40a12019-11-20 20:43:01 -050027 [b'hell', b'a', b'z', b'k', b'spec'],
28 b'hello_from a very special place'),
Ned Nguyenbf081d02018-12-18 16:41:31 -070029 [True, True, False, False, True])
30
31 def testModuleRewrite(self):
32 tmp_gconv_module = os.path.join(self.tempdir, 'gconv-modules')
33
34 data = """
35# from to module cost
36alias FOO charset_foo
37alias BAR charset_bar
38module charset_foo charset_bar UNUSED_MODULE
39
40# from to module cost
41alias CHAR_A charset_A
42alias EUROPE charset_B
43module charset_A charset_B USED_MODULE
44module charset_foo charset_A USED_MODULE
45"""
46 osutils.WriteFile(tmp_gconv_module, data)
47
48 gmods = gconv_strip.GconvModules(tmp_gconv_module)
Mike Frysingerbdd40a12019-11-20 20:43:01 -050049 self.assertEqual(gmods.Load(), [
50 'BAR', 'CHAR_A', 'EUROPE', 'FOO', 'charset_A', 'charset_B',
51 'charset_bar', 'charset_foo'])
Ned Nguyenbf081d02018-12-18 16:41:31 -070052 self.PatchObject(gconv_strip.lddtree, 'ParseELF', return_value={})
Mike Frysinger9c927782019-10-14 02:48:48 -040053 class _StubStat(object):
Mike Frysinger8e303f02020-02-14 22:53:11 -050054 """Fake for lstat."""
Mike Frysinger9c927782019-10-14 02:48:48 -040055 st_size = 0
56 self.PatchObject(gconv_strip.os, 'lstat', return_value=_StubStat)
Ned Nguyenbf081d02018-12-18 16:41:31 -070057 self.PatchObject(gconv_strip.os, 'unlink')
58 gmods.Rewrite(['charset_A', 'charset_B'], dry_run=False)
59
60 expected = """
61# from to module cost
62alias FOO charset_foo
63
64# from to module cost
65alias CHAR_A charset_A
66alias EUROPE charset_B
67module charset_A charset_B USED_MODULE
68module charset_foo charset_A USED_MODULE
69"""
70
71 content = osutils.ReadFile(tmp_gconv_module)
Mike Frysinger2d589a12019-08-25 14:15:12 -040072 self.assertEqual(content, expected)