1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
|
import json
from typing import Callable
import pytest
from pytest_mock import MockFixture
from update_vim_plugins.nix import License, UrlSource
from update_vim_plugins.plugin import GitHubPlugin, VimPlugin
from update_vim_plugins.spec import PluginSpec
@pytest.fixture()
def mock_source(sha256: str):
class MockSource:
def __init__(self, *args, **kwargs):
pass
def get_nix_expression(self):
return "src"
return MockSource()
@pytest.fixture()
def mock_plugin(mock_source):
class MockVimPlugin(VimPlugin):
def __init__(self):
self.name = "test"
self.version = "1.0.0"
self.source = mock_source
self.description = "No description"
self.homepage = "https://example.com"
self.license = License.UNKNOWN
return MockVimPlugin()
def test_vim_plugin_nix_expression(mock_plugin):
assert (
mock_plugin.get_nix_expression()
== 'test = buildVimPluginFrom2Nix { pname = "test"; version = "1.0.0"; src = src; meta = with lib; { description = "No description"; homepage = "https://example.com"; license = with licenses; [ ]; }; };'
)
class MockResponse:
def __init__(self, status_code: int, content: bytes):
self.status_code = status_code
self.content = content
def json(self):
return json.loads(self.content)
def mock_request_get(repsonses: dict[str, MockResponse]):
respones_not_found = MockResponse(404, b'{"message": "Not Found"}')
def mock_get(url: str, *args, **kwargs):
return repsonses.get(url, respones_not_found)
return mock_get
@pytest.fixture()
def github_commits_response():
return MockResponse(
200,
json.dumps(
{
"sha": "6dcb09b5b57875f334f61aebed695e2e4193db5e",
"commit": {
"committer": {
"date": "2011-04-14T16:00:49Z",
},
},
}
),
)
@pytest.fixture()
def github_get(github_commits_response: MockResponse):
repos_response = MockResponse(
200,
json.dumps(
{
"html_url": "https://github.com/octocat/Hello-World",
"description": "This your first repo!",
"fork": False,
"default_branch": "master",
"license": {
"spdx_id": "MIT",
},
}
),
)
responses = {
"https://api.github.com/repos/octocat/Hello-World": repos_response,
"https://api.github.com/repos/octocat/Hello-World/commits/master": github_commits_response,
}
return mock_request_get(responses)
@pytest.fixture()
def github_get_no_license(github_commits_response: MockResponse):
repos_response = MockResponse(
200,
json.dumps(
{
"html_url": "https://github.com/octocat/Hello-World",
"description": "This your first repo!",
"fork": False,
"default_branch": "master",
}
),
)
responses = {
"https://api.github.com/repos/octocat/Hello-World": repos_response,
"https://api.github.com/repos/octocat/Hello-World/commits/master": github_commits_response,
}
return mock_request_get(responses)
def test_github_plugin(mocker: MockFixture, github_get: Callable, url_source: UrlSource):
mocker.patch("requests.get", github_get)
url_source = mocker.patch("update_vim_plugins.nix.UrlSource", url_source)
spec = PluginSpec.from_spec("octocat/Hello-World")
plugin = GitHubPlugin(spec)
assert plugin.name == "Hello-World"
assert plugin.version == "2011-04-14"
assert plugin.description == "This your first repo!"
assert plugin.homepage == "https://github.com/octocat/Hello-World"
assert plugin.license == License.MIT
def test_github_plugin_no_license(mocker: MockFixture, github_get_no_license: Callable, url_source: UrlSource):
mocker.patch("requests.get", github_get_no_license)
url_source = mocker.patch("update_vim_plugins.nix.UrlSource", url_source)
spec = PluginSpec.from_spec("octocat/Hello-World")
plugin = GitHubPlugin(spec)
assert plugin.license == License.UNKNOWN
|