1
1
"""Tests for the bumpversion.click_config module."""
2
2
3
3
from pathlib import Path
4
+ from typing import Optional
4
5
5
6
import click
6
7
import httpx
7
8
import pytest
8
9
9
- from bumpversion .click_config import config_option , resolve_conf_location , download_url
10
+ from bumpversion .click_config import config_option , download_url , resolve_conf_location
10
11
from bumpversion .exceptions import BadInputError
11
12
12
13
13
14
@click .command ()
14
15
@config_option ()
15
- def hello (config : Path ):
16
+ def hello (config : Optional [ Path ] ):
16
17
"""Say hello."""
17
18
click .echo (f"Hello { config } !" )
18
- click .echo (f"path exists: { config .exists ()} " )
19
+ if config :
20
+ click .echo (f"path exists: { config .exists ()} " )
21
+ else :
22
+ click .echo ("Nothing was passed." )
19
23
20
24
21
25
class TestConfigOption :
22
26
"""Tests to for config_option."""
23
27
24
- def test_config_option (self , runner ):
25
- """Passing an invalid option should raise an error."""
28
+ def test_passing_missing_file_raises_error (self , runner ):
29
+ """Explicitly passing a path that does not exist should raise an error."""
26
30
result = runner .invoke (hello , ["--config" , "idont/exist.txt" ])
27
31
32
+ assert result .exit_code != 0
33
+
34
+ def test_passing_nothing_returns_none (self , runner ):
35
+ """If nothing is passed, the value is `None`."""
36
+ result = runner .invoke (hello , [])
37
+
38
+ assert result .exit_code == 0
39
+ assert "Hello None!" in result .output
40
+
41
+ def test_passing_existing_file_returns_path (self , runner , fixtures_path : Path ):
42
+ """Passing an existing file path should return a Path object."""
43
+ config_path = fixtures_path / "basic_cfg.toml"
44
+ result = runner .invoke (hello , ["--config" , str (config_path )])
45
+
46
+ assert result .exit_code == 0
47
+ assert f"Hello { config_path } !" in result .output
48
+
49
+ def test_passing_url_returns_path (self , runner , fixtures_path : Path , httpserver ):
50
+ """Passing an existing file path should return a Path object."""
51
+ # Assemble
52
+ config = fixtures_path .joinpath ("basic_cfg.toml" ).read_text ()
53
+ httpserver .serve_content (config )
54
+
55
+ # Act
56
+ result = runner .invoke (hello , ["--config" , f"{ httpserver .url } /basic_cfg.toml" ])
57
+
58
+ # Assert
28
59
assert result .exit_code == 0
29
- assert "path exists: False " in result .output
60
+ assert "path exists: True " in result .output
30
61
31
62
32
63
class TestResolveConfLocation :
@@ -39,45 +70,37 @@ def test_resolves_file_path(self, fixtures_path: Path):
39
70
assert isinstance (result , Path )
40
71
assert result == dummy_path
41
72
42
- def test_resolves_valid_url (self , mocker ):
73
+ def test_resolves_valid_url (self , fixtures_path : Path , httpserver ):
43
74
"""Should download the file if given a valid URL."""
44
75
# Arrange
45
- mocked_download = mocker .patch (
46
- "bumpversion.click_config.download_url" , return_value = Path ("/tmp/downloaded_config.txt" )
47
- )
48
- url = "http://example.com/config.txt"
76
+ config = fixtures_path .joinpath ("basic_cfg.toml" ).read_text ()
77
+ httpserver .serve_content (config )
78
+ url = f"{ httpserver .url } /basic_cfg.toml"
49
79
50
80
# Act
51
81
result = resolve_conf_location (url )
52
82
53
83
# Assert
54
- mocked_download .assert_called_once_with (url )
55
84
assert isinstance (result , Path )
56
- assert str ( result ) == "/tmp/downloaded_config.txt"
85
+ assert result . read_text ( ) == config
57
86
58
87
59
88
class TestDownloadUrl :
60
89
"""Tests for download_url."""
61
90
62
- def test_download_url_success (self , mocker , tmp_path : Path ):
91
+ def test_download_url_success (self , fixtures_path : Path , httpserver ):
63
92
"""Should return a Path for a valid request."""
64
93
# Arrange
65
- mock_response = mocker .Mock (spec = httpx .Response )
66
- mock_response .status_code = 200
67
- mock_response .text = "content"
68
- mocker .patch ("httpx.get" , return_value = mock_response )
69
- tmp_file = tmp_path / "tempfile.txt"
70
- mock_tempfile = mocker .patch ("bumpversion.click_config.NamedTemporaryFile" , autospec = True )
71
- temp_file_instance = mock_tempfile .return_value .__enter__ .return_value
72
- temp_file_instance .name = str (tmp_file )
94
+ config = fixtures_path .joinpath ("basic_cfg.toml" ).read_text ()
95
+ httpserver .serve_content (config )
96
+ url = f"{ httpserver .url } /basic_cfg.toml"
73
97
74
98
# Act
75
- result = download_url ("http://example.com/config.txt" )
99
+ result = download_url (url )
76
100
77
101
# Assert
78
102
assert isinstance (result , Path )
79
- assert result == tmp_file
80
- mock_tempfile .assert_called_once ()
103
+ assert result .read_text () == config
81
104
82
105
def test_download_url_request_error (self , mocker ):
83
106
"""Should raise BadInputError for a RequestError."""
0 commit comments