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

56 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 

10import configparser 

11from tempfile import TemporaryDirectory 

12 

13import pytest 

14from github.GithubException import GithubException 

15 

16from care.utils.github import GitHubEmo 

17 

18test_repo = 'eclipse-dash/quevee' 

19 

20file_conf = 'eclipse_conf.ini' 

21 

22def setup_credentials(): 

23 # Read .ini config file. 

24 parser = configparser.ConfigParser(interpolation=None) 

25 try: 

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

27 data = f.read() 

28 parser.read_string(data) 

29 except Exception as e: 

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

31 exit() 

32 

33 # Get the GH token from the config file. 

34 section = parser["GitHub"] 

35 gh_token = section["gh_token"] 

36 

37 # Get the GL token from the config file. 

38 section = parser["GitLab"] 

39 gl_token = section["gl_token"] 

40 

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

42 return creds 

43 

44def test_gh_init_env_nok(): 

45 os.environ["GH_AUTH_TOKEN"] = "github_pat_" 

46 gh = GitHubEmo() 

47 assert gh is not None, "GH handle is initialized." 

48 # Any call to gh will raise an exception if it's not correctly authenticated. 

49 with pytest.raises(GithubException) as e: 

50 login = gh.gh.get_user().login 

51 

52 

53def test_gh_init_ok(): 

54 # Unset env var if it exists. 

55 if 'GH_AUTH_TOKEN' in os.environ: 

56 print("Found GH_AUTH_TOKEN in os.environ") 

57 os.environ.pop('GH_AUTH_TOKEN') 

58 

59 creds = setup_credentials() 

60 gh = GitHubEmo(credentials=creds) 

61 assert gh is not None, "GH handle is initialized." 

62 gh_repo = gh.gh.get_repo(test_repo) 

63 assert gh_repo.name == "quevee", "Retrieving quevee repository successfully." 

64 

65 

66def test_gh_contents_root(): 

67 creds = setup_credentials() 

68 gh = GitHubEmo(credentials=creds) 

69 files = gh.get_content_root(test_repo) 

70 assert len(files) > 5 

71 assert 'README.md' in files 

72 

73 

74def test_gh_contents_recursive(): 

75 creds = setup_credentials() 

76 gh = GitHubEmo(credentials=creds) 

77 files = gh.get_content_recursive(test_repo) 

78 assert len(files) > 5 

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

80 

81 

82def test_gh_get_file(): 

83 creds = setup_credentials() 

84 gh = GitHubEmo(credentials=creds) 

85 with TemporaryDirectory() as tmpdir: 

86 gh_file = gh.get_file(test_repo, tmpdir, 'LICENSE') 

87 assert gh_file is not None