Coverage for tests/test_github_action.py: 100%

49 statements  

« prev     ^ index     » next       coverage.py v7.14.3, created at 2026-06-27 17:58 +0000

1# SPDX-FileCopyrightText: 2026 Arcangelo Massari <github@a.arcangelomassari.com> 

2# 

3# SPDX-License-Identifier: ISC 

4 

5from __future__ import annotations 

6 

7from pathlib import Path 

8 

9import pytest 

10import requests_mock 

11 

12from software_citation_sync import github_action 

13 

14SPDX_COPYRIGHT_NONE = "# SPDX-FileCopyrightText: NONE" 

15SPDX_LICENSE_CC0 = "# SPDX-License-" + "Identifier: CC0-1.0" 

16SAVE_URL = ( 

17 "https://archive.softwareheritage.org/api/1/origin/save/git/url/https%3A%2F%2Fgithub.com%2Farcangelo7%2Framose/" 

18) 

19REQUEST_URL = "https://archive.softwareheritage.org/api/1/origin/save/2373061/" 

20SNAPSHOT_SWHID = "swh:1:snp:fc0b501f594531b2b8f694469cc38aed15897bbe" 

21SWH_URL = f"https://archive.softwareheritage.org/{SNAPSHOT_SWHID};origin=https://github.com/arcangelo7/ramose" 

22 

23 

24def mock_software_heritage_archive(requests_mock: requests_mock.Mocker) -> None: 

25 requests_mock.post( 

26 SAVE_URL, 

27 json={ 

28 "request_url": REQUEST_URL, 

29 "save_task_status": "scheduled", 

30 "snapshot_swhid": "", 

31 }, 

32 ) 

33 requests_mock.get( 

34 REQUEST_URL, 

35 json={ 

36 "request_url": REQUEST_URL, 

37 "save_task_status": "succeeded", 

38 "snapshot_swhid": SNAPSHOT_SWHID, 

39 }, 

40 ) 

41 

42 

43def test_action_updates_files_archives_and_sets_changed_output( 

44 tmp_path: Path, 

45 monkeypatch: pytest.MonkeyPatch, 

46 requests_mock: requests_mock.Mocker, 

47) -> None: 

48 citation_path = tmp_path / "CITATION.cff" 

49 readme_path = tmp_path / "README.md" 

50 output_path = tmp_path / "github-output" 

51 citation_path.write_text( 

52 "\n".join( 

53 [ 

54 SPDX_COPYRIGHT_NONE, 

55 SPDX_LICENSE_CC0, 

56 "", 

57 "cff-version: 1.2.0", 

58 "message: If you use this software, please cite it using these metadata.", 

59 "title: RAMOSE", 

60 "authors:", 

61 " - family-names: Massari", 

62 " given-names: Arcangelo", 

63 "", 

64 ], 

65 ), 

66 encoding="utf-8", 

67 ) 

68 readme_path.write_text("# RAMOSE\n", encoding="utf-8") 

69 (tmp_path / "pyproject.toml").write_text('[project]\nname = "ramose"\nversion = "2.8.0"\n', encoding="utf-8") 

70 mock_software_heritage_archive(requests_mock) 

71 monkeypatch.chdir(tmp_path) 

72 monkeypatch.setattr(github_action, "_current_date", lambda: "2026-06-25") 

73 monkeypatch.setattr(github_action, "ARCHIVE_POLL_INTERVAL_SECONDS", 0) 

74 monkeypatch.setenv("GITHUB_SERVER_URL", "https://github.com") 

75 monkeypatch.setenv("GITHUB_REPOSITORY", "arcangelo7/ramose") 

76 monkeypatch.setenv("GITHUB_OUTPUT", str(output_path)) 

77 

78 github_action.main() 

79 

80 assert output_path.read_text(encoding="utf-8") == "\n".join( 

81 [ 

82 "changed=true", 

83 "", 

84 ], 

85 ) 

86 assert citation_path.read_text(encoding="utf-8") == "\n".join( 

87 [ 

88 SPDX_COPYRIGHT_NONE, 

89 SPDX_LICENSE_CC0, 

90 "", 

91 "cff-version: 1.2.0", 

92 "message: If you use this software, please cite it using these metadata.", 

93 "title: RAMOSE", 

94 "authors:", 

95 " - family-names: Massari", 

96 " given-names: Arcangelo", 

97 "version: 2.8.0", 

98 "date-released: '2026-06-25'", 

99 f"url: {SWH_URL}", 

100 "", 

101 ], 

102 ) 

103 assert readme_path.read_text(encoding="utf-8") == "\n".join( 

104 [ 

105 "# RAMOSE", 

106 "", 

107 "<!-- software-citation-action:start -->", 

108 "To cite the latest version of this software (2.8.0), use this BibTeX entry:", 

109 "", 

110 "```bibtex", 

111 "@software{RAMOSE-2.8.0,", 

112 "author = {Massari, Arcangelo},", 

113 "title = {RAMOSE},", 

114 f"url = {{{SWH_URL}}},", 

115 "version = {2.8.0},", 

116 "year = {2026}", 

117 "}", 

118 "```", 

119 "<!-- software-citation-action:end -->", 

120 "", 

121 ], 

122 ) 

123 

124 

125def test_action_creates_missing_citation( 

126 tmp_path: Path, 

127 monkeypatch: pytest.MonkeyPatch, 

128 requests_mock: requests_mock.Mocker, 

129) -> None: 

130 citation_path = tmp_path / "CITATION.cff" 

131 readme_path = tmp_path / "README.md" 

132 output_path = tmp_path / "github-output" 

133 readme_path.write_text("# RAMOSE\n", encoding="utf-8") 

134 (tmp_path / "pyproject.toml").write_text( 

135 "\n".join( 

136 [ 

137 "[project]", 

138 'name = "ramose"', 

139 'version = "2.8.0"', 

140 'authors = [{ name = "Arcangelo Massari", email = "github@a.arcangelomassari.com" }]', 

141 "", 

142 ], 

143 ), 

144 encoding="utf-8", 

145 ) 

146 mock_software_heritage_archive(requests_mock) 

147 monkeypatch.chdir(tmp_path) 

148 monkeypatch.setattr(github_action, "_current_date", lambda: "2026-06-25") 

149 monkeypatch.setattr(github_action, "ARCHIVE_POLL_INTERVAL_SECONDS", 0) 

150 monkeypatch.setenv("GITHUB_SERVER_URL", "https://github.com") 

151 monkeypatch.setenv("GITHUB_REPOSITORY", "arcangelo7/ramose") 

152 monkeypatch.setenv("GITHUB_OUTPUT", str(output_path)) 

153 

154 github_action.main() 

155 

156 assert output_path.read_text(encoding="utf-8") == "\n".join( 

157 [ 

158 "changed=true", 

159 "", 

160 ], 

161 ) 

162 assert citation_path.read_text(encoding="utf-8") == "\n".join( 

163 [ 

164 "cff-version: 1.2.0", 

165 "message: If you use this software, please cite it using these metadata.", 

166 "title: ramose", 

167 "authors:", 

168 " - name: Arcangelo Massari", 

169 "version: 2.8.0", 

170 "date-released: '2026-06-25'", 

171 f"url: {SWH_URL}", 

172 "", 

173 ], 

174 ) 

175 assert readme_path.read_text(encoding="utf-8") == "\n".join( 

176 [ 

177 "# RAMOSE", 

178 "", 

179 "<!-- software-citation-action:start -->", 

180 "To cite the latest version of this software (2.8.0), use this BibTeX entry:", 

181 "", 

182 "```bibtex", 

183 "@software{ramose-2.8.0,", 

184 "author = {Arcangelo Massari},", 

185 "title = {ramose},", 

186 f"url = {{{SWH_URL}}},", 

187 "version = {2.8.0},", 

188 "year = {2026}", 

189 "}", 

190 "```", 

191 "<!-- software-citation-action:end -->", 

192 "", 

193 ], 

194 )