1#!/usr/bin/env python3 2# Copyright 2023 The ChromiumOS Authors 3# Use of this source code is governed by a BSD-style license that can be 4# found in the LICENSE file. 5 6"""Tests for generate_llvm_next_pgo.""" 7 8from pathlib import Path 9import shutil 10import tempfile 11import textwrap 12import unittest 13 14# This script's name makes lines exceed 80 chars if it's not imported `as` 15# something shorter. 16import create_chroot_and_generate_pgo_profile as create_chroot_etc 17 18 19class Test(unittest.TestCase): 20 """Tests for generate_llvm_next_pgo.""" 21 22 def make_tempdir(self) -> Path: 23 tempdir = Path(tempfile.mkdtemp(prefix="generate_llvm_next_pgo_test_")) 24 self.addCleanup(lambda: shutil.rmtree(tempdir)) 25 return tempdir 26 27 def test_path_translation_works(self): 28 repo_root = Path("/some/repo") 29 chroot_info = create_chroot_etc.ChrootInfo( 30 chroot_name="my-chroot", 31 out_dir_name="my-out", 32 ) 33 self.assertEqual( 34 create_chroot_etc.translate_chroot_path_to_out_of_chroot( 35 repo_root, "/tmp/file/path", chroot_info 36 ), 37 repo_root / "my-out" / "tmp/file/path", 38 ) 39 40 def test_llvm_ebuild_location(self): 41 tempdir = self.make_tempdir() 42 43 llvm_subdir = ( 44 tempdir / "src/third_party/chromiumos-overlay/sys-devel/llvm" 45 ) 46 want_ebuild = llvm_subdir / "llvm-18.0.0_pre12345.ebuild" 47 files = [ 48 llvm_subdir / "llvm-15.ebuild", 49 llvm_subdir / "llvm-16.0.1-r3.ebuild", 50 want_ebuild, 51 llvm_subdir / "llvm-9999.ebuild", 52 ] 53 54 llvm_subdir.mkdir(parents=True) 55 for f in files: 56 f.touch() 57 58 self.assertEqual( 59 create_chroot_etc.locate_current_llvm_ebuild(tempdir), 60 want_ebuild, 61 ) 62 63 def test_llvm_hash_parsing(self): 64 h = create_chroot_etc.parse_llvm_next_hash( 65 textwrap.dedent( 66 """\ 67 # Copyright blah blah 68 EAPI=7 69 LLVM_HASH="98f5a340975bc00197c57e39eb4ca26e2da0e8a2" # r496208 70 LLVM_NEXT_HASH="14f0776550b5a49e1c42f49a00213f7f3fa047bf" # r498229 71 # Snip 72 CROS_WORKON_COMMIT=("${LLVM_NEXT_HASH}") 73 """ 74 ) 75 ) 76 77 self.assertEqual(h, "14f0776550b5a49e1c42f49a00213f7f3fa047bf") 78 79 80if __name__ == "__main__": 81 unittest.main() 82