1# This Source Code Form is subject to the terms of the Mozilla Public 2# License, v. 2.0. If a copy of the MPL was not distributed with this 3# file, You can obtain one at http://mozilla.org/MPL/2.0/. 4 5from __future__ import print_function, unicode_literals 6 7import os 8from argparse import Namespace 9from collections import defaultdict 10from textwrap import dedent 11 12from manifestparser import TestManifest 13 14import mozunit 15import pytest 16from conftest import setup_args 17 18 19@pytest.fixture 20def get_active_tests(setup_test_harness, parser): 21 setup_test_harness(*setup_args) 22 runtests = pytest.importorskip('runtests') 23 md = runtests.MochitestDesktop('plain', {'log_tbpl': '-'}) 24 25 options = vars(parser.parse_args([])) 26 27 def inner(**kwargs): 28 opts = options.copy() 29 opts.update(kwargs) 30 31 manifest = opts.get('manifestFile') 32 if isinstance(manifest, basestring): 33 md.testRootAbs = os.path.dirname(manifest) 34 elif isinstance(manifest, TestManifest): 35 md.testRootAbs = manifest.rootdir 36 37 md._active_tests = None 38 md.prefs_by_manifest = defaultdict(set) 39 return md, md.getActiveTests(Namespace(**opts)) 40 41 return inner 42 43 44@pytest.fixture 45def create_manifest(tmpdir, build_obj): 46 47 def inner(string, name='manifest.ini'): 48 manifest = tmpdir.join(name) 49 manifest.write(string, ensure=True) 50 path = unicode(manifest) 51 return TestManifest(manifests=(path,), strict=False, rootdir=tmpdir.strpath) 52 53 return inner 54 55 56def test_prefs_validation(get_active_tests, create_manifest): 57 # Test prefs set in a single manifest. 58 manifest_relpath = 'manifest.ini' 59 manifest = create_manifest(dedent(""" 60 [DEFAULT] 61 prefs= 62 foo=bar 63 browser.dom.foo=baz 64 65 [files/test_pass.html] 66 [files/test_fail.html] 67 """)) 68 69 options = { 70 'runByManifest': True, 71 'manifestFile': manifest, 72 } 73 md, tests = get_active_tests(**options) 74 75 assert len(tests) == 2 76 assert manifest_relpath in md.prefs_by_manifest 77 78 prefs = md.prefs_by_manifest[manifest_relpath] 79 assert len(prefs) == 1 80 assert prefs.pop() == "\nfoo=bar\nbrowser.dom.foo=baz" 81 82 # Test prefs set with runByManifest disabled. 83 options['runByManifest'] = False 84 with pytest.raises(SystemExit): 85 get_active_tests(**options) 86 87 # Test prefs set in non-default section. 88 options['runByManifest'] = True 89 options['manifestFile'] = create_manifest(dedent(""" 90 [files/test_pass.html] 91 prefs=foo=bar 92 [files/test_fail.html] 93 """)) 94 with pytest.raises(SystemExit): 95 get_active_tests(**options) 96 97 98def test_prefs_validation_with_ancestor_manifest(get_active_tests, create_manifest): 99 # Test prefs set by an ancestor manifest. 100 create_manifest(dedent(""" 101 [DEFAULT] 102 prefs= 103 foo=bar 104 browser.dom.foo=baz 105 106 [files/test_pass.html] 107 [files/test_fail.html] 108 """), name='subdir/manifest.ini') 109 110 manifest = create_manifest(dedent(""" 111 [DEFAULT] 112 prefs = 113 browser.dom.foo=fleem 114 flower=rose 115 116 [include:manifest.ini] 117 [test_foo.html] 118 """), name='subdir/ancestor-manifest.ini') 119 120 options = { 121 'runByManifest': True, 122 'manifestFile': manifest, 123 } 124 125 md, tests = get_active_tests(**options) 126 assert len(tests) == 3 127 128 key = os.path.join('subdir', 'ancestor-manifest.ini') 129 assert key in md.prefs_by_manifest 130 prefs = md.prefs_by_manifest[key] 131 assert len(prefs) == 1 132 assert prefs.pop() == '\nbrowser.dom.foo=fleem\nflower=rose' 133 134 key = '{}:{}'.format( 135 os.path.join('subdir', 'ancestor-manifest.ini'), 136 os.path.join('subdir', 'manifest.ini') 137 ) 138 assert key in md.prefs_by_manifest 139 prefs = md.prefs_by_manifest[key] 140 assert len(prefs) == 1 141 assert prefs.pop() == '\nbrowser.dom.foo=fleem\nflower=rose \nfoo=bar\nbrowser.dom.foo=baz' 142 143 144if __name__ == '__main__': 145 mozunit.main() 146