blob: 9b7e3881c959b9f786d9012c411fc567817ff479 [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
Greg Edelstona4c9b3b2020-01-07 17:51:13 -070017pytestmark = cros_test_lib.pytestmark_inside_only
18
Ned Nguyenbf081d02018-12-18 16:41:31 -070019
Mike Frysinger8e303f02020-02-14 22:53:11 -050020assert sys.version_info >= (3, 6), 'This module requires Python 3.6+'
21
22
Ned Nguyenbf081d02018-12-18 16:41:31 -070023class GconvStriptTest(cros_test_lib.MockTempDirTestCase):
24 """Tests for gconv_strip script."""
25
26 def testMultipleStringMatch(self):
Mike Frysinger2d589a12019-08-25 14:15:12 -040027 self.assertEqual(
Ned Nguyenbf081d02018-12-18 16:41:31 -070028 gconv_strip.MultipleStringMatch(
Mike Frysingerbdd40a12019-11-20 20:43:01 -050029 [b'hell', b'a', b'z', b'k', b'spec'],
30 b'hello_from a very special place'),
Ned Nguyenbf081d02018-12-18 16:41:31 -070031 [True, True, False, False, True])
32
33 def testModuleRewrite(self):
34 tmp_gconv_module = os.path.join(self.tempdir, 'gconv-modules')
35
36 data = """
37# from to module cost
38alias FOO charset_foo
39alias BAR charset_bar
40module charset_foo charset_bar UNUSED_MODULE
41
42# from to module cost
43alias CHAR_A charset_A
44alias EUROPE charset_B
45module charset_A charset_B USED_MODULE
46module charset_foo charset_A USED_MODULE
47"""
48 osutils.WriteFile(tmp_gconv_module, data)
49
50 gmods = gconv_strip.GconvModules(tmp_gconv_module)
Mike Frysingerbdd40a12019-11-20 20:43:01 -050051 self.assertEqual(gmods.Load(), [
52 'BAR', 'CHAR_A', 'EUROPE', 'FOO', 'charset_A', 'charset_B',
53 'charset_bar', 'charset_foo'])
Ned Nguyenbf081d02018-12-18 16:41:31 -070054 self.PatchObject(gconv_strip.lddtree, 'ParseELF', return_value={})
Mike Frysinger9c927782019-10-14 02:48:48 -040055 class _StubStat(object):
Mike Frysinger8e303f02020-02-14 22:53:11 -050056 """Fake for lstat."""
Mike Frysinger9c927782019-10-14 02:48:48 -040057 st_size = 0
58 self.PatchObject(gconv_strip.os, 'lstat', return_value=_StubStat)
Ned Nguyenbf081d02018-12-18 16:41:31 -070059 self.PatchObject(gconv_strip.os, 'unlink')
60 gmods.Rewrite(['charset_A', 'charset_B'], dry_run=False)
61
62 expected = """
63# from to module cost
64alias FOO charset_foo
65
66# from to module cost
67alias CHAR_A charset_A
68alias EUROPE charset_B
69module charset_A charset_B USED_MODULE
70module charset_foo charset_A USED_MODULE
71"""
72
73 content = osutils.ReadFile(tmp_gconv_module)
Mike Frysinger2d589a12019-08-25 14:15:12 -040074 self.assertEqual(content, expected)