11import json
22import os
33import textwrap
4+ from time import sleep
45
56import pytest
67
78from tome .internal .cache import TomePaths
8- from tome .internal .utils .files import mkdir
9+ from tome .internal .utils .files import mkdir , rmdir
910
1011from tests .utils .tools import TestClient
1112
1213
1314@pytest .fixture
1415def client ():
1516 client = TestClient ()
16- # Editable commands
17- client .run ("new mynamespace:mycommand" )
18- client .run ("install . -e" )
17+
1918 mkdir (os .path .join (client .current_folder , "greetings" ))
19+ mkdir (os .path .join (client .current_folder , "deployments" ))
20+
2021 tome_script = textwrap .dedent ("""
2122 from tome.command import tome_command
2223
@@ -36,10 +37,7 @@ def bye(tome_api, parser, *args):
3637 print(f"bye: {args.message}")
3738 """ )
3839 client .save ({os .path .join (client .current_folder , "greetings" , "greetings-commands.py" ): tome_script })
39- # Cache commands
40- tome_scripts_path = TomePaths (client .cache_folder ).scripts_path
41- cache_commands_folder = os .path .join (tome_scripts_path , "all" , "deployments" )
42- mkdir (cache_commands_folder )
40+
4341 tome_script = textwrap .dedent ("""
4442 from tome.command import tome_command
4543
@@ -58,8 +56,16 @@ def release(tome_api, parser, *args):
5856 args = parser.parse_args(*args)
5957 print(f"Release: {args.message}")
6058 """ )
61- client .save ({os .path .join (cache_commands_folder , "deployments-commands.py" ): tome_script })
59+ client .save ({os .path .join ("deployments" , "deployments-commands.py" ): tome_script })
6260 client .run ("install ." )
61+
62+ rmdir (os .path .join (client .current_folder , "greetings" ))
63+ rmdir (os .path .join (client .current_folder , "deployments" ))
64+
65+ # Editable commands
66+ client .run ("new mynamespace:mycommand" )
67+ client .run ("install . -e" )
68+
6369 return client
6470
6571
@@ -100,7 +106,7 @@ def test_empty_pattern():
100106 """
101107 client = TestClient ()
102108 client .run ("list" )
103- assert "Error: No matches were found for * pattern." in client .out
109+ assert "No matches were found for '*' pattern." in client .out
104110
105111
106112def test_list_failed_imported ():
@@ -144,15 +150,110 @@ def mycommand(tome_api, parser, *args):
144150
145151def test_formats_json ():
146152 client = TestClient ()
147- client .run (f "new mynamespace:mycommand" )
153+ client .run ("new mynamespace:mycommand" )
148154 client .run ("install ." )
149155 client .run ("list --format json" )
150156
157+ origin_key = os .path .abspath (client .current_folder )
158+ namespace_key = "mynamespace"
159+ command_key = "mycommand"
160+
151161 expected_output = {
152- "results" : {
153- "mynamespace" : {"mycommand" : {"doc" : "Description of the command." , "type" : "cache" , "error" : None }}
154- },
155- "pattern" : "*" ,
162+ origin_key : {
163+ namespace_key : {command_key : {"doc" : "Description of the command." , "type" : "cache" , "error" : None }}
164+ }
156165 }
157166
158167 assert json .loads (client .out ) == expected_output
168+
169+
170+ def test_grouped_output ():
171+ client = TestClient ()
172+ client .run (f"new namespace1:mycommand1" )
173+ client .run ("install ." )
174+
175+ rmdir (os .path .join (client .current_folder , "namespace1" ))
176+
177+ with client .chdir (os .path .join (client .current_folder , "editable-commands" )):
178+ client .run (f"new namespace2:mycommand-editable" )
179+ client .run ("install . -e" )
180+
181+ git_repo_folder = os .path .join (client .current_folder , "git_repo" )
182+ with client .chdir (git_repo_folder ):
183+ client .run ("new namespace3:mycommand-git" )
184+
185+ client .init_git_repo (folder = git_repo_folder )
186+
187+ install_source = f"{ os .path .join (client .current_folder , git_repo_folder )} /.git"
188+ client .run (f"install '{ install_source } '" )
189+
190+ expected = {
191+ os .path .abspath (client .current_folder ): {
192+ "namespace1" : {"mycommand1" : {"doc" : "Description of the command." , "type" : "cache" , "error" : None }}
193+ },
194+ os .path .abspath (os .path .join (client .current_folder , "git_repo" , ".git" )): {
195+ "namespace3" : {"mycommand-git" : {"doc" : "Description of the command." , "type" : "cache" , "error" : None }}
196+ },
197+ os .path .abspath (os .path .join (client .current_folder , "editable-commands" )): {
198+ "namespace2" : {
199+ "mycommand-editable" : {"doc" : "Description of the command." , "type" : "editable" , "error" : None }
200+ }
201+ },
202+ }
203+
204+ client .run ("list --format=json" )
205+ assert json .loads (client .out ) == expected
206+
207+
208+ def test_overlapped_commands ():
209+ client = TestClient ()
210+
211+ # FIXME: right now if command names overlap the first one that was installed will be the one used
212+ # should we error out if commands overlap? should we allow multiple commands with the same name?
213+ # let's wait for the user to ask for this feature
214+
215+ with client .chdir (os .path .join (client .current_folder , "someorigin" )):
216+ client .run (f"new namespace:mycommand" )
217+ client .run ("install ." )
218+
219+ sleep (0.1 )
220+ with client .chdir (os .path .join (client .current_folder , "anotherorigin" )):
221+ client .run (f"new namespace:mycommand" )
222+ client .run ("install ." )
223+
224+ expected = {
225+ os .path .abspath (os .path .join (client .current_folder , "someorigin" )): {
226+ "namespace" : {"mycommand" : {"doc" : "Description of the command." , "type" : "cache" , "error" : None }}
227+ }
228+ }
229+
230+ client .run ("list --format=json" )
231+ assert json .loads (client .out ) == expected
232+
233+ sleep (0.1 )
234+ with client .chdir (os .path .join (client .current_folder , "yetanotherorigin" )):
235+ client .run (f"new namespace:mycommand" )
236+ client .run ("install ." )
237+
238+ client .run ("list --format=json" )
239+ assert json .loads (client .out ) == expected
240+
241+ sleep (0.1 )
242+ with client .chdir (os .path .join (client .current_folder , "lastorigin" )):
243+ client .run (f"new namespace:mycommand" )
244+ client .run ("install ." )
245+
246+ client .run ("list --format=json" )
247+ assert json .loads (client .out ) == expected
248+
249+ # let's uninstall the first one, then the next should be used
250+
251+ expected = {
252+ os .path .abspath (os .path .join (client .current_folder , "anotherorigin" )): {
253+ "namespace" : {"mycommand" : {"doc" : "Description of the command." , "type" : "cache" , "error" : None }}
254+ }
255+ }
256+
257+ client .run (f"uninstall '{ os .path .abspath (os .path .join (client .current_folder , 'someorigin' ))} '" )
258+ client .run ("list --format=json" )
259+ assert json .loads (client .out ) == expected
0 commit comments