Coverage for tests / eclipse / care / utils / test_gitlab.py: 95%

64 statements  

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

1# Copyright (c) 2025 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 

9import os 

10from pprint import pprint 

11import configparser 

12 

13from tempfile import TemporaryDirectory 

14 

15from care.utils.gitlab import GitLabEmo 

16 

17test_group = 'eclipse/technology/dash' 

18test_repo = test_group + '/documentation-util' 

19 

20 

21file_conf = 'eclipse_conf.ini' 

22 

23def setup_credentials(): 

24 """Set up required init file for credentials. 

25 

26 Returns: 

27 dict: A dict with the tokens needed for GH and GL. 

28 """ 

29 # Read .ini config file. 

30 parser = configparser.ConfigParser(interpolation=None) 

31 try: 

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

33 data = f.read() 

34 parser.read_string(data) 

35 except Exception as e: 

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

37 exit() 

38 

39 # Get the GH token from the config file. 

40 section = parser["GitHub"] 

41 gh_token = section["gh_token"] 

42 

43 # Get the GL token from the config file. 

44 section = parser["GitLab"] 

45 gl_token = section["gl_token"] 

46 

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

48 return creds 

49 

50# Wrong token, should not connect. 

51def test_gl_init_auth_nok(): 

52 os.environ["GL_AUTH_TOKEN"] = "gitlab_pat_" 

53 gle = GitLabEmo(verbose=True) 

54 gl = gle.get_gl() 

55 pprint(gl) 

56 assert gl is not None 

57 assert gl.user is None 

58 assert gl.oauth_token is None 

59 

60 

61def test_gl_init_auth_ok(): 

62 creds = setup_credentials() 

63 

64 # Unset env var if it exists. 

65 if 'GL_AUTH_TOKEN' in os.environ: 

66 print("Found GL_AUTH_TOKEN in os.environ") 

67 os.environ.pop('GL_AUTH_TOKEN') 

68 

69 gle = GitLabEmo(credentials=creds, verbose=True) 

70 assert gle is not None, "GL handle is initialized." 

71 gl_user = gle.get_gl().user 

72 assert gl_user.username == 'bbaldassari2kd' 

73 

74 

75def test_gl_repos(): 

76 gle = GitLabEmo() 

77 repos = gle.get_repos(test_group) 

78 assert len(repos) < 15 

79 assert len(repos) > 5 

80 

81 

82def test_gl_contents_root(): 

83 gle = GitLabEmo() 

84 files = gle.get_content_root(test_repo) 

85 assert len(files) > 5 

86 assert 'README.md' not in files 

87 assert 'LICENSE' in files 

88 assert 'CONTRIBUTING.md' in files 

89 

90 

91def test_gl_contents_recursive(): 

92 gle = GitLabEmo() 

93 files = gle.get_content_recursive(test_repo) 

94 assert len(files) > 5 

95 assert not any(f['name'] == 'README.md' for f in files) 

96 assert any(f['name'] == 'LICENSE' for f in files) 

97 assert any(f['name'] == 'CONTRIBUTING.md' for f in files) 

98 

99 

100def test_gl_get_file(): 

101 gle = GitLabEmo() 

102 with TemporaryDirectory() as tmpdir: 

103 gl_file = gle.get_file(test_repo, tmpdir, 'LICENSE') 

104 assert gl_file is not None