1#!/usr/bin/env python3 2# 3# Copyright 2019, The Android Open Source Project 4# 5# Licensed under the Apache License, Version 2.0 (the "License"); 6# you may not use this file except in compliance with the License. 7# You may obtain a copy of the License at 8# 9# http://www.apache.org/licenses/LICENSE-2.0 10# 11# Unless required by applicable law or agreed to in writing, software 12# distributed under the License is distributed on an "AS IS" BASIS, 13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14# See the License for the specific language governing permissions and 15# limitations under the License. 16# 17 18""" 19Unit tests for the compiler.py script. 20 21Install: 22 $> sudo apt-get install python3-pytest ## OR 23 $> pip install -U pytest 24See also https://docs.pytest.org/en/latest/getting-started.html 25 26Usage: 27 $> pytest compiler_test.py 28 29See also https://docs.pytest.org/en/latest/usage.html 30""" 31import os 32 33import compiler_ri as compiler 34 35DIR = os.path.abspath(os.path.dirname(__file__)) 36TEXTCACHE = os.path.join(DIR, 'test_fixtures/compiler/common_textcache') 37SYSTRACE = os.path.join(DIR, 'test_fixtures/compiler/common_systrace') 38ARGV = [os.path.join(DIR, 'compiler.py'), '-i', TEXTCACHE, '-t', SYSTRACE] 39PERFETTO_TRACE = os.path.join(DIR, 40 'test_fixtures/compiler/common_perfetto_trace.pb') 41 42def assert_compile_result(output, expected, *extra_argv): 43 argv = ARGV + ['-o', output] + [args for args in extra_argv] 44 45 compiler.main(argv) 46 47 with open(output, 'rb') as f1, open(expected, 'rb') as f2: 48 assert f1.read() == f2.read() 49 50### Unit tests - testing compiler code directly 51def test_transform_perfetto_trace_to_systrace(tmpdir): 52 expected = os.path.join(DIR, 53 'test_fixtures/compiler/test_result_systrace') 54 output = tmpdir.mkdir('compiler').join('tmp_systrace') 55 56 compiler.transform_perfetto_trace_to_systrace(PERFETTO_TRACE, str(output)) 57 58 with open(output, 'rb') as f1, open(expected, 'rb') as f2: 59 assert f1.read() == f2.read() 60 61### Functional tests - calls 'compiler.py --args...' 62def test_compiler_main(tmpdir): 63 output = tmpdir.mkdir('compiler').join('output') 64 65 # No duration 66 expected = os.path.join(DIR, 67 'test_fixtures/compiler/test_result_without_duration.TraceFile.pb') 68 assert_compile_result(output, expected) 69 70 # 10ms duration 71 expected = os.path.join(DIR, 72 'test_fixtures/compiler/test_result_with_duration.TraceFile.pb') 73 assert_compile_result(output, expected, '--duration', '10000') 74 75 # 30ms duration 76 expected = os.path.join(DIR, 77 'test_fixtures/compiler/test_result_without_duration.TraceFile.pb') 78 assert_compile_result(output, expected, '--duration', '30000') 79