1#!/usr/bin/env python 2# Copyright 2017 The Chromium Authors 3# Use of this source code is governed by a BSD-style license that can be 4# found in the LICENSE file. 5 6import json 7import os 8import shutil 9import sys 10import tempfile 11import unittest 12 13import common_merge_script_tests 14 15THIS_DIR = os.path.dirname(__file__) 16 17sys.path.insert( 18 0, os.path.abspath(os.path.join(THIS_DIR, '..', 'resources'))) 19import noop_merge 20 21 22class NoopMergeTest(unittest.TestCase): 23 24 # pylint: disable=super-with-arguments 25 def setUp(self): 26 super(NoopMergeTest, self).setUp() 27 self.temp_dir = tempfile.mkdtemp() 28 # pylint: enable=super-with-arguments 29 30 # pylint: disable=super-with-arguments 31 def tearDown(self): 32 shutil.rmtree(self.temp_dir) 33 super(NoopMergeTest, self).tearDown() 34 # pylint: enable=super-with-arguments 35 36 def test_copies_first_json(self): 37 input_json = os.path.join(self.temp_dir, 'input.json') 38 input_json_contents = {'foo': ['bar', 'baz']} 39 with open(input_json, 'w') as f: 40 json.dump(input_json_contents, f) 41 output_json = os.path.join(self.temp_dir, 'output.json') 42 self.assertEqual(0, noop_merge.noop_merge(output_json, [input_json])) 43 with open(output_json) as f: 44 self.assertEqual(input_json_contents, json.load(f)) 45 46 def test_no_jsons(self): 47 output_json = os.path.join(self.temp_dir, 'output.json') 48 self.assertEqual(0, noop_merge.noop_merge(output_json, [])) 49 with open(output_json) as f: 50 self.assertEqual({}, json.load(f)) 51 52 def test_multiple_jsons(self): 53 input_json1 = os.path.join(self.temp_dir, 'input1.json') 54 input_json1_contents = {'test1': ['foo1', 'bar1']} 55 with open(input_json1, 'w') as f: 56 json.dump(input_json1_contents, f) 57 input_json2 = os.path.join(self.temp_dir, 'input2.json') 58 input_json2_contents = {'test2': ['foo2', 'bar2']} 59 with open(input_json2, 'w') as f: 60 json.dump(input_json2_contents, f) 61 output_json = os.path.join(self.temp_dir, 'output.json') 62 self.assertNotEqual( 63 0, noop_merge.noop_merge(output_json, [input_json1, input_json2])) 64 65 66class CommandLineTest(common_merge_script_tests.CommandLineTest): 67 # pylint: disable=super-with-arguments 68 def __init__(self, methodName='runTest'): 69 super(CommandLineTest, self).__init__(methodName, noop_merge) 70 # pylint: enable=super-with-arguments 71 72if __name__ == '__main__': 73 unittest.main() 74