1
- import os
1
+ import pytest
2
2
from click .testing import CliRunner
3
3
from cffconvert .cli .cli import cli as cffconvert
4
+ from tests .cli .helpers import get_formats
5
+ from tests .cli .helpers import read_sibling_file
4
6
5
7
6
- def read_sibling_file (filename ):
7
- fixture = os .path .join (os .path .dirname (__file__ ), filename )
8
- with open (fixture , "rt" , encoding = "utf-8" ) as f :
9
- return f .read ()
8
+ @pytest .fixture (scope = "module" )
9
+ def cffstr ():
10
+ return read_sibling_file (__file__ , "CITATION.cff" )
10
11
11
12
12
13
def test_local_cff_file_does_not_exist ():
@@ -23,7 +24,7 @@ def test_printing_of_help():
23
24
with runner .isolated_filesystem ():
24
25
result = runner .invoke (cffconvert , ["--help" ])
25
26
assert result .exit_code == 0
26
- assert result .output [: 6 ] == "Usage:"
27
+ assert result .output . startswith ( "Usage:" )
27
28
28
29
29
30
def test_printing_of_version ():
@@ -34,90 +35,24 @@ def test_printing_of_version():
34
35
assert result .output == "3.0.0a0\n "
35
36
36
37
37
- def test_printing_on_stdout_as_bibtex ():
38
- cffstr = read_sibling_file ( "CITATION.cff" )
39
- expected = read_sibling_file ("bibtex.bib" )
38
+ @ pytest . mark . parametrize ( "fmt, fname" , get_formats ())
39
+ def test_printing_on_stdout ( fmt , fname , cffstr ):
40
+ expected = read_sibling_file (__file__ , fname )
40
41
runner = CliRunner ()
41
42
with runner .isolated_filesystem ():
42
- with open ("CITATION.cff" , "wt" , encoding = "utf-8" ) as f :
43
- f .write (cffstr )
44
- result = runner .invoke (cffconvert , ["-f" , "bibtex" ])
45
- assert result .exit_code == 0
46
- actual = result .output
47
- assert expected == actual
48
-
49
-
50
- def test_printing_on_stdout_as_cff ():
51
- cffstr = read_sibling_file ("CITATION.cff" )
52
- expected = cffstr
53
- runner = CliRunner ()
54
- with runner .isolated_filesystem ():
55
- with open ("CITATION.cff" , "wt" , encoding = "utf-8" ) as f :
56
- f .write (cffstr )
57
- result = runner .invoke (cffconvert , ["-f" , "cff" ])
58
- assert result .exit_code == 0
59
- actual = result .output
60
- assert expected == actual
61
-
62
-
63
- def test_printing_on_stdout_as_codemeta ():
64
- cffstr = read_sibling_file ("CITATION.cff" )
65
- expected = read_sibling_file ("codemeta.json" )
66
- runner = CliRunner ()
67
- with runner .isolated_filesystem ():
68
- with open ("CITATION.cff" , "wt" , encoding = "utf-8" ) as f :
69
- f .write (cffstr )
70
- result = runner .invoke (cffconvert , ["-f" , "codemeta" ])
71
- assert result .exit_code == 0
72
- actual = result .output
73
- assert expected == actual
74
-
75
-
76
- def test_printing_on_stdout_as_endnote ():
77
- cffstr = read_sibling_file ("CITATION.cff" )
78
- expected = read_sibling_file ("endnote.enw" )
79
- runner = CliRunner ()
80
- with runner .isolated_filesystem ():
81
- with open ("CITATION.cff" , "wt" , encoding = "utf-8" ) as f :
82
- f .write (cffstr )
83
- result = runner .invoke (cffconvert , ["-f" , "endnote" ])
84
- assert result .exit_code == 0
85
- actual = result .output
86
- assert expected == actual
87
-
88
-
89
- def test_printing_on_stdout_as_ris ():
90
- cffstr = read_sibling_file ("CITATION.cff" )
91
- expected = read_sibling_file ("ris.txt" )
92
- runner = CliRunner ()
93
- with runner .isolated_filesystem ():
94
- with open ("CITATION.cff" , "wt" , encoding = "utf-8" ) as f :
95
- f .write (cffstr )
96
- result = runner .invoke (cffconvert , ["-f" , "ris" ])
97
- assert result .exit_code == 0
98
- actual = result .output
99
- assert expected == actual
100
-
101
-
102
- def test_printing_on_stdout_as_schemaorg ():
103
- cffstr = read_sibling_file ("CITATION.cff" )
104
- expected = read_sibling_file ("schemaorg.json" )
105
- runner = CliRunner ()
106
- with runner .isolated_filesystem ():
107
- with open ("CITATION.cff" , "wt" , encoding = "utf-8" ) as f :
108
- f .write (cffstr )
109
- result = runner .invoke (cffconvert , ["-f" , "schema.org" ])
43
+ with open ("CITATION.cff" , "wt" , encoding = "utf-8" ) as fid :
44
+ fid .write (cffstr )
45
+ result = runner .invoke (cffconvert , ["-f" , fmt ])
110
46
assert result .exit_code == 0
111
47
actual = result .output
112
48
assert expected == actual
113
49
114
50
115
- def test_raising_error_on_unsupported_format ():
116
- cffstr = read_sibling_file ("CITATION.cff" )
51
+ def test_raising_error_on_unsupported_format (cffstr ):
117
52
runner = CliRunner ()
118
53
with runner .isolated_filesystem ():
119
- with open ("CITATION.cff" , "wt" , encoding = "utf-8" ) as f :
120
- f .write (cffstr )
54
+ with open ("CITATION.cff" , "wt" , encoding = "utf-8" ) as fid :
55
+ fid .write (cffstr )
121
56
result = runner .invoke (cffconvert , ["-f" , "unsupported_97491" ])
122
57
assert result .exit_code == 2
123
58
assert "Error: Invalid value for '-f'" in str (result .output )
@@ -132,71 +67,15 @@ def test_without_arguments():
132
67
assert result .exception .strerror == "No such file or directory"
133
68
134
69
135
- def test_writing_as_bibtex ():
136
- cffstr = read_sibling_file ("CITATION.cff" )
137
- expected = read_sibling_file ("bibtex.bib" )
138
- runner = CliRunner ()
139
- with runner .isolated_filesystem ():
140
- with open ("CITATION.cff" , "wt" , encoding = "utf-8" ) as f :
141
- f .write (cffstr )
142
- result = runner .invoke (cffconvert , ["-f" , "bibtex" , "-o" , "bibtex.bib" ])
143
- with open ("bibtex.bib" , "rt" , encoding = "utf-8" ) as f :
144
- actual = f .read ()
145
- assert result .exit_code == 0
146
- assert expected == actual
147
-
148
-
149
- def test_writing_as_codemeta ():
150
- cffstr = read_sibling_file ("CITATION.cff" )
151
- expected = read_sibling_file ("codemeta.json" )
152
- runner = CliRunner ()
153
- with runner .isolated_filesystem ():
154
- with open ("CITATION.cff" , "wt" , encoding = "utf-8" ) as f :
155
- f .write (cffstr )
156
- result = runner .invoke (cffconvert , ["-f" , "codemeta" , "-o" , "codemeta.json" ])
157
- with open ("codemeta.json" , "rt" , encoding = "utf-8" ) as f :
158
- actual = f .read ()
159
- assert result .exit_code == 0
160
- assert expected == actual
161
-
162
-
163
- def test_writing_as_endnote ():
164
- cffstr = read_sibling_file ("CITATION.cff" )
165
- expected = read_sibling_file ("endnote.enw" )
166
- runner = CliRunner ()
167
- with runner .isolated_filesystem ():
168
- with open ("CITATION.cff" , "wt" , encoding = "utf-8" ) as f :
169
- f .write (cffstr )
170
- result = runner .invoke (cffconvert , ["-f" , "endnote" , "-o" , "endnote.enw" ])
171
- with open ("endnote.enw" , "rt" , encoding = "utf-8" ) as f :
172
- actual = f .read ()
173
- assert result .exit_code == 0
174
- assert expected == actual
175
-
176
-
177
- def test_writing_as_ris ():
178
- cffstr = read_sibling_file ("CITATION.cff" )
179
- expected = read_sibling_file ("ris.txt" )
180
- runner = CliRunner ()
181
- with runner .isolated_filesystem ():
182
- with open ("CITATION.cff" , "wt" , encoding = "utf-8" ) as f :
183
- f .write (cffstr )
184
- result = runner .invoke (cffconvert , ["-f" , "ris" , "-o" , "ris.txt" ])
185
- with open ("ris.txt" , "rt" , encoding = "utf-8" ) as f :
186
- actual = f .read ()
187
- assert result .exit_code == 0
188
- assert expected == actual
189
-
190
-
191
- def test_writing_as_schemaorg ():
192
- cffstr = read_sibling_file ("CITATION.cff" )
193
- expected = read_sibling_file ("schemaorg.json" )
70
+ @pytest .mark .parametrize ("fmt, fname" , get_formats ())
71
+ def test_writing_to_file (fmt , fname , cffstr ):
72
+ expected = read_sibling_file (__file__ , fname )
194
73
runner = CliRunner ()
195
74
with runner .isolated_filesystem ():
196
- with open ("CITATION.cff" , "wt" , encoding = "utf-8" ) as f :
197
- f .write (cffstr )
198
- result = runner .invoke (cffconvert , ["-f" , "schema.org" , "-o" , "schemaorg.json" ])
199
- with open ("schemaorg.json" , "rt" , encoding = "utf-8" ) as f :
200
- actual = f .read ()
75
+ with open ("CITATION.cff" , "wt" , encoding = "utf-8" ) as fid :
76
+ fid .write (cffstr )
77
+ result = runner .invoke (cffconvert , ["-f" , fmt , "-o" , fname ])
78
+ with open (fname , "rt" , encoding = "utf-8" ) as fid :
79
+ actual = fid .read ()
201
80
assert result .exit_code == 0
202
81
assert expected == actual
0 commit comments