Coverage for tests/test_sync.py: 100%

39 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 

10 

11from software_citation_sync.config import CitationConfig, ProjectMetadata 

12from software_citation_sync.readme import write_readme_block 

13from software_citation_sync.sync import check, write_citation_metadata, write_readme 

14 

15SPDX_COPYRIGHT_NONE = "# SPDX-FileCopyrightText: NONE" 

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

17SWH_URL = ( 

18 "https://archive.softwareheritage.org/swh:1:snp:fc0b501f594531b2b8f694469cc38aed15897bbe" 

19 ";origin=https://github.com/arcangelo7/ramose" 

20) 

21 

22 

23def test_write_updates_citation_and_readme(tmp_path: Path) -> None: 

24 citation_path = tmp_path / "CITATION.cff" 

25 readme_path = tmp_path / "README.md" 

26 # REUSE-IgnoreStart 

27 citation_path.write_text( 

28 "\n".join( 

29 [ 

30 SPDX_COPYRIGHT_NONE, 

31 SPDX_LICENSE_CC0, 

32 "", 

33 "cff-version: 1.2.0", 

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

35 "title: RAMOSE", 

36 "authors:", 

37 " - family-names: Massari", 

38 " given-names: Arcangelo", 

39 "version: 2.0.0", 

40 "date-released: '2026-04-03'", 

41 "", 

42 ], 

43 ), 

44 encoding="utf-8", 

45 ) 

46 # REUSE-IgnoreEnd 

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

48 

49 config = CitationConfig( 

50 citation_path=citation_path, 

51 readme_path=readme_path, 

52 version="2.8.0", 

53 date_released="2026-06-25", 

54 software_heritage_url=SWH_URL, 

55 ) 

56 citation_changed = write_citation_metadata(config) 

57 readme_changed = write_readme(config) 

58 

59 assert citation_changed is True 

60 assert readme_changed is True 

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

62 [ 

63 SPDX_COPYRIGHT_NONE, 

64 SPDX_LICENSE_CC0, 

65 "", 

66 "cff-version: 1.2.0", 

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

68 "title: RAMOSE", 

69 "authors:", 

70 " - family-names: Massari", 

71 " given-names: Arcangelo", 

72 "version: 2.8.0", 

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

74 f"url: {SWH_URL}", 

75 "", 

76 ], 

77 ) 

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

79 [ 

80 "# RAMOSE", 

81 "", 

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

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

84 "", 

85 "```bibtex", 

86 "@software{RAMOSE-2.8.0,", 

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

88 "title = {RAMOSE},", 

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

90 "version = {2.8.0},", 

91 "year = {2026}", 

92 "}", 

93 "```", 

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

95 "", 

96 ], 

97 ) 

98 

99 

100def test_write_creates_missing_citation(tmp_path: Path) -> None: 

101 citation_path = tmp_path / "CITATION.cff" 

102 

103 citation_changed = write_citation_metadata( 

104 CitationConfig( 

105 citation_path=citation_path, 

106 readme_path=tmp_path / "README.md", 

107 version="2.8.0", 

108 date_released="2026-06-25", 

109 software_heritage_url=SWH_URL, 

110 ), 

111 ProjectMetadata( 

112 title="ramose", 

113 authors=("Arcangelo Massari",), 

114 ), 

115 ) 

116 

117 assert citation_changed is True 

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

119 [ 

120 "cff-version: 1.2.0", 

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

122 "title: ramose", 

123 "authors:", 

124 " - name: Arcangelo Massari", 

125 "version: 2.8.0", 

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

127 f"url: {SWH_URL}", 

128 "", 

129 ], 

130 ) 

131 

132 

133def test_check_reports_readme_block_error(tmp_path: Path) -> None: 

134 citation_path = tmp_path / "CITATION.cff" 

135 readme_path = tmp_path / "README.md" 

136 # REUSE-IgnoreStart 

137 citation_path.write_text( 

138 "\n".join( 

139 [ 

140 "cff-version: 1.2.0", 

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

142 "title: RAMOSE", 

143 "authors:", 

144 " - family-names: Massari", 

145 " given-names: Arcangelo", 

146 "version: 2.8.0", 

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

148 f"url: {SWH_URL}", 

149 "", 

150 ], 

151 ), 

152 encoding="utf-8", 

153 ) 

154 # REUSE-IgnoreEnd 

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

156 

157 result = check( 

158 CitationConfig( 

159 citation_path=citation_path, 

160 readme_path=readme_path, 

161 version="2.8.0", 

162 date_released="2026-06-25", 

163 software_heritage_url=SWH_URL, 

164 ), 

165 ) 

166 

167 assert result.errors == (f"{readme_path} citation block is out of date",) 

168 

169 

170def test_write_readme_rejects_incomplete_markers(tmp_path: Path) -> None: 

171 readme_path = tmp_path / "README.md" 

172 readme_path.write_text("# RAMOSE\n\n<!-- software-citation-action:start -->\n", encoding="utf-8") 

173 

174 with pytest.raises( 

175 ValueError, 

176 match=r"^README citation block markers are incomplete or out of order$", 

177 ) as exc_info: 

178 write_readme_block(readme_path, "2.8.0", "@software{RAMOSE-2.8.0}") 

179 

180 assert str(exc_info.value) == "README citation block markers are incomplete or out of order"