Coverage for tests/conftest.py: 100%

42 statements  

« prev     ^ index     » next       coverage.py v7.2.2, created at 2023-03-30 09:04 +0000

1import os 

2from pathlib import Path 

3import unittest 

4 

5import pytest 

6import tests.subprocess_mock # pylint: disable=unused-import 

7import tests.docker_mock # pylint: disable=unused-import 

8from tests.requests_mock import add_gitea_mocks, add_github_mocks, Mock 

9import tests.jayporeci_patch # pylint: disable=unused-import 

10 

11from jaypore_ci import jci, executors, remotes, reporters, repos 

12 

13 

14def idfn(x): 

15 name = [] 

16 for _, item in sorted(x.items()): 

17 what, _, cls = str(item).replace(">", "").split(".")[-3:] 

18 name.append(".".join([what, cls])) 

19 return str(name) 

20 

21 

22def factory(*, repo, remote, executor, reporter): 

23 "Return a new pipeline every time the builder function is called" 

24 

25 def build(): 

26 r = repo.from_env() 

27 return jci.Pipeline( 

28 poll_interval=0, 

29 repo=r, 

30 remote=remote.from_env(repo=r), 

31 executor=executor(), 

32 reporter=reporter(), 

33 ) 

34 

35 return build 

36 

37 

38def set_env_keys(): 

39 os.environ["JAYPORE_GITEA_TOKEN"] = "fake_gitea_token" 

40 os.environ["JAYPORE_GITHUB_TOKEN"] = "fake_github_token" 

41 os.environ["JAYPORE_EMAIL_ADDR"] = "fake@email.com" 

42 os.environ["JAYPORE_EMAIL_PASSWORD"] = "fake_email_password" 

43 os.environ["JAYPORE_EMAIL_TO"] = "fake.to@mymailmail.com" 

44 

45 

46@pytest.fixture( 

47 scope="function", 

48 params=list( 

49 jci.Pipeline.env_matrix( 

50 reporter=[reporters.Text, reporters.Markdown], 

51 remote=[ 

52 remotes.Mock, 

53 remotes.Email, 

54 remotes.GitRemote, 

55 remotes.Gitea, 

56 remotes.Github, 

57 ], 

58 repo=[repos.Git], 

59 executor=[executors.Docker], 

60 ) 

61 ), 

62 ids=idfn, 

63) 

64def pipeline(request): 

65 set_env_keys() 

66 builder = factory( 

67 repo=request.param["repo"], 

68 remote=request.param["remote"], 

69 executor=request.param["executor"], 

70 reporter=request.param["reporter"], 

71 ) 

72 if request.param["remote"] == remotes.Gitea and not Mock.gitea_added: 

73 add_gitea_mocks(builder().remote) 

74 if request.param["remote"] == remotes.Github and not Mock.github_added: 

75 add_github_mocks(builder().remote) 

76 if request.param["remote"] == remotes.Email: 

77 with unittest.mock.patch("smtplib.SMTP_SSL", autospec=True): 

78 yield builder 

79 else: 

80 yield builder 

81 

82 

83@pytest.fixture( 

84 scope="function", 

85 params=list((Path(__name__) / "../docs/source/examples").resolve().glob("*.py")), 

86 ids=str, 

87) 

88def doc_example_filepath(request): 

89 set_env_keys() 

90 yield request.param