main
py 92 lines 3.1 KB
Raw
1 # Copyright 2026 Google LLC
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 from unittest.mock import patch, mock_open, MagicMock
16 from typer.testing import CliRunner
17 import pytest
18
19 from colab_cli.cli import app
20
21 runner = CliRunner()
22
23
24 @pytest.fixture
25 def mock_resources():
26 with patch("importlib.resources.files") as mock:
27 yield mock
28
29
30 def test_readme_from_resources(mock_resources):
31 mock_readme = MagicMock()
32 mock_readme.is_file.return_value = True
33 mock_readme.read_text.return_value = "Fake README content"
34
35 def joinpath_side_effect(name):
36 if name == "README.md":
37 return mock_readme
38 return MagicMock(is_file=MagicMock(return_value=False))
39
40 mock_resources.return_value.joinpath.side_effect = joinpath_side_effect
41
42 result = runner.invoke(app, ["README"])
43 assert result.exit_code == 0
44 assert result.output.strip() == "Fake README content"
45 mock_resources.assert_called_once_with("colab_cli")
46 mock_resources.return_value.joinpath.assert_called_with("README.md")
47
48
49 def test_skill_from_resources(mock_resources):
50 mock_skill = MagicMock()
51 mock_skill.is_file.return_value = True
52 mock_skill.read_text.return_value = "Fake SKILL content"
53
54 def joinpath_side_effect(name):
55 if name == "SKILL.md":
56 return mock_skill
57 return MagicMock(is_file=MagicMock(return_value=False))
58
59 mock_resources.return_value.joinpath.side_effect = joinpath_side_effect
60
61 result = runner.invoke(app, ["SKILL"])
62 assert result.exit_code == 0
63 assert result.output.strip() == "Fake SKILL content"
64 mock_resources.assert_called_once_with("colab_cli")
65 mock_resources.return_value.joinpath.assert_called_with("SKILL.md")
66
67
68 def test_readme_fallback_to_file(mock_resources):
69 mock_resources.side_effect = Exception("No resources")
70
71 import builtins
72
73 real_open = builtins.open
74
75 def mock_open_impl(file, *args, **kwargs):
76 if "README.md" in str(file):
77 return mock_open(read_data="Fake local README")(*args, **kwargs)
78 return real_open(file, *args, **kwargs)
79
80 with patch("os.path.exists", return_value=True):
81 with patch("builtins.open", side_effect=mock_open_impl):
82 result = runner.invoke(app, ["README"])
83 assert result.exit_code == 0
84 assert result.output.strip() == "Fake local README"
85
86
87 def test_readme_failure(mock_resources):
88 mock_resources.side_effect = Exception("No resources")
89 with patch("os.path.exists", return_value=False):
90 result = runner.invoke(app, ["README"])
91 assert result.exit_code == 1
92 assert "README.md content not available" in result.output