Coverage for tsfpga/vivado/test/test_common.py: 100%

29 statements  

« prev     ^ index     » next       coverage.py v7.6.9, created at 2024-12-07 20:51 +0000

1# -------------------------------------------------------------------------------------------------- 

2# Copyright (c) Lukas Vik. All rights reserved. 

3# 

4# This file is part of the tsfpga project, a project platform for modern FPGA development. 

5# https://tsfpga.com 

6# https://github.com/tsfpga/tsfpga 

7# -------------------------------------------------------------------------------------------------- 

8 

9# Standard libraries 

10from pathlib import Path 

11from unittest.mock import patch 

12 

13# First party libraries 

14from tsfpga.vivado.common import get_git_sha_slv, get_vivado_version, run_vivado_tcl 

15 

16THIS_DIR = Path(__file__).parent 

17 

18 

19def test_run_vivado_tcl(): 

20 vivado_path = THIS_DIR / "vivado.exe" 

21 tcl_file = THIS_DIR / "script.tcl" 

22 expected_cmd = [ 

23 str(vivado_path.resolve()), 

24 "-mode", 

25 "batch", 

26 "-notrace", 

27 "-source", 

28 str(tcl_file.resolve()), 

29 ] 

30 

31 with patch("tsfpga.vivado.common.Process") as mocked_process: 

32 mocked_process.NonZeroExitCode = ValueError 

33 assert run_vivado_tcl(vivado_path, tcl_file) 

34 mocked_process.assert_called_once_with(args=expected_cmd, cwd=THIS_DIR) 

35 

36 with patch("tsfpga.vivado.common.Process") as mocked_process: 

37 mocked_process.NonZeroExitCode = ValueError 

38 assert run_vivado_tcl(vivado_path, tcl_file, no_log_file=True) 

39 mocked_process.assert_called_once_with( 

40 args=expected_cmd + ["-nojournal", "-nolog"], cwd=THIS_DIR 

41 ) 

42 

43 with patch("tsfpga.vivado.common.Process") as mocked_process: 

44 mocked_process.NonZeroExitCode = ValueError 

45 mocked_process.return_value.consume_output.side_effect = ValueError("Non-zero exit code!") 

46 assert not run_vivado_tcl(vivado_path, tcl_file, no_log_file=True) 

47 

48 

49def test_get_vivado_version(): 

50 assert ( 

51 get_vivado_version(vivado_path=Path("/home/lukas/work/Xilinx/Vivado/2021.2/bin/vivado")) 

52 == "2021.2" 

53 ) 

54 

55 

56def test_get_git_sha_slv(): 

57 with patch("tsfpga.vivado.common.get_git_sha") as get_git_sha: 

58 get_git_sha.return_value = "abcdef0123456789" 

59 assert get_git_sha_slv(git_directory=None) == ( 

60 "10101011110011011110111100000001", 

61 "00100011010001010110011110001001", 

62 ) 

63 

64 with patch("tsfpga.vivado.common.get_git_sha") as get_git_sha: 

65 get_git_sha.return_value = "00abcdef00123400" 

66 assert get_git_sha_slv(git_directory=None) == ( 

67 "00000000101010111100110111101111", 

68 "00000000000100100011010000000000", 

69 )