Skip to content

Commit 68c6ad6

Browse files
authored
Merge pull request #351 from citation-file-format/clitests
cli tests
2 parents 47892b6 + 8398272 commit 68c6ad6

13 files changed

+234
-579
lines changed

tests/cli/cff_1_0_3/.zenodo.json

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
{
2+
"creators": [
3+
{
4+
"affiliation": "Netherlands eScience Center",
5+
"name": "Spaaks, Jurriaan H."
6+
},
7+
{
8+
"affiliation": "Netherlands eScience Center",
9+
"name": "Klaver, Tom"
10+
}
11+
],
12+
"keywords": [
13+
"citation",
14+
"bibliography",
15+
"cff",
16+
"CITATION.cff"
17+
],
18+
"license": {
19+
"id": "Apache-2.0"
20+
},
21+
"publication_date": "2018-01-16",
22+
"related_identifiers": [
23+
{
24+
"identifier": "10.5281/zenodo.1162057",
25+
"relation": "isSupplementedBy",
26+
"scheme": "doi"
27+
},
28+
{
29+
"identifier": "https://github.com/citation-file-format/cffconvert",
30+
"relation": "isSupplementedBy",
31+
"scheme": "url"
32+
}
33+
],
34+
"title": "cffconvert",
35+
"upload_type": "software",
36+
"version": "1.0.0"
37+
}

tests/cli/cff_1_0_3/apalike.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Spaaks J.H., Klaver T. (2018). cffconvert (version 1.0.0). DOI: 10.5281/zenodo.1162057 URL: https://github.com/citation-file-format/cffconvert

tests/cli/cff_1_0_3/test_cli.py

+24-145
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
import os
1+
import pytest
22
from click.testing import CliRunner
33
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
46

57

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")
1011

1112

1213
def test_local_cff_file_does_not_exist():
@@ -23,7 +24,7 @@ def test_printing_of_help():
2324
with runner.isolated_filesystem():
2425
result = runner.invoke(cffconvert, ["--help"])
2526
assert result.exit_code == 0
26-
assert result.output[:6] == "Usage:"
27+
assert result.output.startswith("Usage:")
2728

2829

2930
def test_printing_of_version():
@@ -34,90 +35,24 @@ def test_printing_of_version():
3435
assert result.output == "3.0.0a0\n"
3536

3637

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)
4041
runner = CliRunner()
4142
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])
11046
assert result.exit_code == 0
11147
actual = result.output
11248
assert expected == actual
11349

11450

115-
def test_raising_error_on_unsupported_format():
116-
cffstr = read_sibling_file("CITATION.cff")
51+
def test_raising_error_on_unsupported_format(cffstr):
11752
runner = CliRunner()
11853
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)
12156
result = runner.invoke(cffconvert, ["-f", "unsupported_97491"])
12257
assert result.exit_code == 2
12358
assert "Error: Invalid value for '-f'" in str(result.output)
@@ -132,71 +67,15 @@ def test_without_arguments():
13267
assert result.exception.strerror == "No such file or directory"
13368

13469

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)
19473
runner = CliRunner()
19574
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()
20180
assert result.exit_code == 0
20281
assert expected == actual

tests/cli/cff_1_1_0/.zenodo.json

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
{
2+
"creators": [
3+
{
4+
"affiliation": "Netherlands eScience Center",
5+
"name": "Spaaks, Jurriaan H."
6+
},
7+
{
8+
"affiliation": "Netherlands eScience Center",
9+
"name": "Klaver, Tom"
10+
}
11+
],
12+
"keywords": [
13+
"citation",
14+
"bibliography",
15+
"cff",
16+
"CITATION.cff"
17+
],
18+
"license": {
19+
"id": "Apache-2.0"
20+
},
21+
"publication_date": "2018-01-16",
22+
"related_identifiers": [
23+
{
24+
"identifier": "10.5281/zenodo.1162057",
25+
"relation": "isSupplementedBy",
26+
"scheme": "doi"
27+
},
28+
{
29+
"identifier": "https://github.com/citation-file-format/cffconvert",
30+
"relation": "isSupplementedBy",
31+
"scheme": "url"
32+
}
33+
],
34+
"title": "cffconvert",
35+
"upload_type": "software",
36+
"version": "1.0.0"
37+
}

tests/cli/cff_1_1_0/apalike.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Spaaks J.H., Klaver T. (2018). cffconvert (version 1.0.0). DOI: 10.5281/zenodo.1162057 URL: https://github.com/citation-file-format/cffconvert

0 commit comments

Comments
 (0)