Coverage for tests / eclipse / care / copyright / test_copyright_headers.py: 96%

68 statements  

« prev     ^ index     » next       coverage.py v7.13.5, created at 2026-03-24 09:38 +0100

1# Copyright (c) 2026 The Eclipse Foundation 

2# 

3# This program and the accompanying materials are made available under the 

4# terms of the Eclipse Public License 2.0 which is available at 

5# http://www.eclipse.org/legal/epl-2.0. 

6# 

7# SPDX-License-Identifier: EPL-2.0 

8 

9from pathlib import Path 

10import configparser 

11 

12import care.copyright.copyright_headers as ch 

13from care.utils.gitlab import GitLabEmo 

14 

15test_project_id = 'technology.packager' 

16test_org = 'eclipse-packager' 

17test_repo_gh = test_org + '/packager' 

18test_group = 'eclipse/technology/dash' 

19test_repo_gl = test_group + '/documentation-util' 

20 

21file_conf = 'eclipse_conf.ini' 

22 

23def setup_credentials(): 

24 # Read .ini config file. 

25 parser = configparser.ConfigParser(interpolation=None) 

26 try: 

27 with open(file_conf, "r") as f: 

28 data = f.read() 

29 parser.read_string(data) 

30 except Exception as e: 

31 print("Error reading " + file_conf + " file:" + str(e) + ".") 

32 exit() 

33 

34 # Get the GH token from the config file. 

35 section = parser["GitHub"] 

36 gh_token = section["gh_token"] 

37 

38 # Get the GL token from the config file. 

39 section = parser["GitLab"] 

40 gl_token = section["gl_token"] 

41 

42 creds = {"GH_TOKEN": gh_token, "GL_TOKEN": gl_token} 

43 return creds 

44 

45 

46def test_load_known_extensions(): 

47 extensions = ch._load_known_extensions() 

48 assert extensions is not None 

49 

50 

51def test_detect_copyright(tmp_path): 

52 test_file = tmp_path / "test_copyright.py" 

53 content = """# Copyright (c) 2026 The Eclipse Foundation 

54# Distributed under the Eclipse Public License 2.0. 

55""" 

56 test_file.write_text(content) 

57 copyright_holders = ch._detect_copyright(Path(test_file)) 

58 assert copyright_holders is not None 

59 assert len(copyright_holders) != 0 

60 

61 

62def test_get_copyright_info(): 

63 creds = setup_credentials() 

64 provider = GitLabEmo(credentials=creds, verbose=True) 

65 percentage_headers, copyright_holders = ch._get_copyright_info(provider, test_repo_gl) 

66 assert percentage_headers >= 0 

67 assert len(copyright_holders) != 0 

68 

69 

70def test_gl_analyze(): 

71 creds = setup_credentials() 

72 results = ch.gl_analyze(test_group, credentials=creds, verbose=True) 

73 assert results is not None 

74 assert results[test_repo_gl]['copyright_headers_percentage'] >= 50 

75 

76 

77def test_gl_analyze_nok(): 

78 creds = setup_credentials() 

79 results = ch.gl_analyze('nonexistent', credentials=creds, verbose=True) 

80 assert results is None 

81 

82 

83def test_gh_analyze(): 

84 creds = setup_credentials() 

85 results = ch.gh_analyze(test_org, credentials=creds, verbose=True) 

86 assert results is not None 

87 assert results[test_repo_gh]['copyright_headers_percentage'] >= 50 

88 

89 

90def test_gh_analyze_nok(): 

91 creds = setup_credentials() 

92 results = ch.gh_analyze('nonexistent', credentials=creds, verbose=True) 

93 assert results is None 

94 

95 

96def test_analyse_project(): 

97 creds = setup_credentials() 

98 results = ch.analyse_project(test_project_id, credentials=creds, verbose=True) 

99 assert results is not None 

100 assert len(results) > 0 

101 

102 

103def test_analyse_project_nok(): 

104 creds = setup_credentials() 

105 results = ch.analyse_project('technology.nonexistent', credentials=creds, verbose=True) 

106 assert results is None