1#!/usr/bin/python 2 3# Copyright 2014 Google Inc. 4# 5# Use of this source code is governed by a BSD-style license that can be 6# found in the LICENSE file. 7 8""" 9Test generate_user_config.py. 10""" 11 12import argparse 13import os 14import shutil 15import sys 16import tempfile 17import test_variables 18import unittest 19import utils 20 21sys.path.append(test_variables.GYP_GEN_DIR) 22 23from generate_user_config import generate_user_config as gen_config 24 25# Name of SkUserConfig file. 26USER_CONFIG_NAME = 'SkUserConfig-h.txt' 27MISSING_FILENAME = 'missing-filename.xxx' 28# Path to unchanging Dummy SkUserConfig file. 29FULL_DUMMY_PATH = os.path.join(os.path.dirname(__file__), 'inputs', 30 USER_CONFIG_NAME) 31REBASELINE_MSG = ('If you\'ve modified generate_user_config.py, run ' 32 '"generate_user_config_tests.py --rebaseline" to rebaseline') 33 34def generate_dummy_user_config(original_sk_user_config, 35 require_sk_user_config, target_dir): 36 # Add an arbitrary set of defines 37 defines = [ 'SK_BUILD_FOR_ANDROID', 38 'SK_BUILD_FOR_ANDROID_FRAMEWORK', 39 'SK_SCALAR_IS_FLOAT', 40 'foo', 41 'bar' ] 42 gen_config(original_sk_user_config=original_sk_user_config, 43 require_sk_user_config=require_sk_user_config, 44 target_dir=target_dir, ordered_set=defines) 45 46 47class GenUserConfigTest(unittest.TestCase): 48 49 def test_missing_sk_user_config(self): 50 tmp = tempfile.mkdtemp() 51 original = os.path.join(tmp, MISSING_FILENAME) 52 assert not os.path.exists(original) 53 54 55 # With require_sk_user_config set to True, an AssertionError will be 56 # thrown when original_sk_user_config is missing. 57 with self.assertRaises(AssertionError): 58 ordered_set = [ 'define' ] 59 gen_config(original_sk_user_config=original, 60 require_sk_user_config=True, 61 target_dir=tmp, ordered_set=ordered_set) 62 63 # With require_sk_user_config set to False, it is okay for 64 # original_sk_user_config to be missing. 65 generate_dummy_user_config(original_sk_user_config=original, 66 require_sk_user_config=False, target_dir=tmp) 67 actual_name = os.path.join(tmp, MISSING_FILENAME) 68 utils.compare_to_expectation(actual_name=actual_name, 69 expectation_name=MISSING_FILENAME, 70 assert_true=self.assertTrue, 71 msg=REBASELINE_MSG) 72 73 shutil.rmtree(tmp) 74 75 def test_gen_config(self): 76 tmp = tempfile.mkdtemp() 77 generate_dummy_user_config(FULL_DUMMY_PATH, True, tmp) 78 actual_name = os.path.join(tmp, USER_CONFIG_NAME) 79 utils.compare_to_expectation(actual_name=actual_name, 80 expectation_name=USER_CONFIG_NAME, 81 assert_true=self.assertTrue, msg=REBASELINE_MSG) 82 shutil.rmtree(tmp) 83 84 85def main(): 86 loader = unittest.TestLoader() 87 suite = loader.loadTestsFromTestCase(GenUserConfigTest) 88 results = unittest.TextTestRunner(verbosity=2).run(suite) 89 print repr(results) 90 if not results.wasSuccessful(): 91 raise Exception('failed one or more unittests') 92 93 94def rebaseline(): 95 generate_dummy_user_config(FULL_DUMMY_PATH, True, utils.EXPECTATIONS_DIR) 96 generate_dummy_user_config(MISSING_FILENAME, False, utils.EXPECTATIONS_DIR) 97 98if __name__ == "__main__": 99 parser = argparse.ArgumentParser() 100 parser.add_argument('-r', '--rebaseline', help='Rebaseline expectations.', 101 action='store_true') 102 args = parser.parse_args() 103 104 if args.rebaseline: 105 rebaseline() 106 else: 107 main() 108 109