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

29 statements  

« prev     ^ index     » next       coverage.py v6.4, created at 2022-05-28 04:01 +0000

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

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

3# 

4# This file is part of the tsfpga project. 

5# https://tsfpga.com 

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

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

8 

9from unittest.mock import patch 

10from pathlib import Path 

11 

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

13 

14THIS_DIR = Path(__file__).parent 

15 

16 

17def test_run_vivado_tcl(): 

18 vivado_path = THIS_DIR / "vivado.exe" 

19 tcl_file = THIS_DIR / "script.tcl" 

20 expected_cmd = [ 

21 str(vivado_path.resolve()), 

22 "-mode", 

23 "batch", 

24 "-notrace", 

25 "-source", 

26 str(tcl_file.resolve()), 

27 ] 

28 

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

30 mocked_process.NonZeroExitCode = ValueError 

31 assert run_vivado_tcl(vivado_path, tcl_file) 

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

33 

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

35 mocked_process.NonZeroExitCode = ValueError 

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

37 mocked_process.assert_called_once_with( 

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

39 ) 

40 

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

42 mocked_process.NonZeroExitCode = ValueError 

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

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

45 

46 

47def test_get_vivado_version(): 

48 assert ( 

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

50 == "2021.2" 

51 ) 

52 

53 

54def test_get_git_sha_slv(): 

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

56 get_git_sha.return_value = "abcdef0123456789" 

57 assert get_git_sha_slv(git_directory=None) == ( 

58 "10101011110011011110111100000001", 

59 "00100011010001010110011110001001", 

60 ) 

61 

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

63 get_git_sha.return_value = "00abcdef00123400" 

64 assert get_git_sha_slv(git_directory=None) == ( 

65 "00000000101010111100110111101111", 

66 "00000000000100100011010000000000", 

67 )