Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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 

9import subprocess 

10 

11import pytest 

12 

13from tsfpga.system_utils import create_file, read_last_lines_of_file, run_command 

14 

15 

16def test_run_command_called_with_nonexisting_binary_should_raise_exception(): 

17 cmd = ["/apa/hest/zebra.exe", "foobar"] 

18 with pytest.raises(FileNotFoundError): 

19 run_command(cmd) 

20 

21 

22def test_run_command_with_non_zero_return_code_should_raise_exception(): 

23 cmd = ["ls", "/apa/hest/zebra"] 

24 with pytest.raises(subprocess.CalledProcessError): 

25 run_command(cmd) 

26 

27 

28def test_run_command_called_with_non_list_should_raise_exception(): 

29 cmd = ["ls", "-la"] 

30 run_command(cmd) 

31 

32 cmd = "ls -la" 

33 with pytest.raises(ValueError) as exception_info: 

34 run_command(cmd) 

35 assert str(exception_info.value).startswith("Must be called with a list") 

36 

37 

38def test_read_last_lines_of_file_with_short_file(tmp_path): 

39 # A file that is smaller than the buffer size 

40 data = "a\nb\nc" 

41 file = create_file(tmp_path / "data.txt", contents=data) 

42 assert read_last_lines_of_file(file, num_lines=10) == data 

43 

44 

45def test_read_last_lines_of_file_with_long_file(tmp_path): 

46 # A file that is larger than the buffer size 

47 data_trash = (("a" * 700) + "\n") * 3000 

48 data_last_lines = (("b" * 700) + "\n") * 10 

49 file = create_file(tmp_path / "data.txt", contents=data_trash + data_last_lines) 

50 assert read_last_lines_of_file(file, num_lines=10) == data_last_lines 

51 

52 

53def test_read_last_lines_of_file_with_trailing_newlines(tmp_path): 

54 # A file that is smaller than the buffer size 

55 data = "a\nb\n\n \n\n" 

56 file = create_file(tmp_path / "data.txt", contents=data) 

57 assert read_last_lines_of_file(file, num_lines=10) == data 

58 

59 

60def test_read_last_lines_of_file_with_empty_file(tmp_path): 

61 data = "" 

62 file = create_file(tmp_path / "data.txt", contents=data) 

63 assert read_last_lines_of_file(file, num_lines=10) == data 

64 

65 data = "\n" 

66 file = create_file(tmp_path / "data.txt", contents=data) 

67 assert read_last_lines_of_file(file, num_lines=10) == data