Skip to content

Commit 11db7a1

Browse files
committed
Add .aspect/axl.axl task to exercise the axl api
1 parent 5057e1a commit 11db7a1

File tree

1 file changed

+205
-0
lines changed

1 file changed

+205
-0
lines changed

.aspect/axl.axl

Lines changed: 205 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,205 @@
1+
"""
2+
Exercise axl
3+
"""
4+
5+
def test(tc, cond, msg) -> int:
6+
if not cond:
7+
fail(msg)
8+
else:
9+
print(msg, "... OK")
10+
return tc + 1
11+
12+
def test_env(ctx: TaskContext, tc: int, temp_dir: str) -> int:
13+
# Test current_exe
14+
current_exe = ctx.std.env.current_exe()
15+
tc = test(tc, current_exe.endswith("/aspect-cli"), "current_exe should end with /aspect-cli")
16+
17+
# Test home_dir
18+
home = ctx.std.env.home_dir()
19+
tc = test(tc, home != None, "home_dir should return a value")
20+
tc = test(tc, type(home) == "string", "home_dir should be a string")
21+
22+
# Test current_dir
23+
current_dir = ctx.std.env.current_dir()
24+
tc = test(tc, type(current_dir) == "string", "current_dir should be a string")
25+
tc = test(tc, ctx.std.fs.is_dir(current_dir), "current_dir should be a directory")
26+
27+
# Test var for existing and non-existing variables
28+
path_var = ctx.std.env.var("PATH")
29+
tc = test(tc, path_var != None, "PATH environment variable should exist")
30+
tc = test(tc, type(path_var) == "string", "PATH should be a string")
31+
no_var = ctx.std.env.var("NO_SUCH_VAR_12345")
32+
tc = test(tc, no_var == None, "Non-existent variable should return None")
33+
34+
# Test vars
35+
all_vars = ctx.std.env.vars()
36+
tc = test(tc, type(all_vars) == "list", "vars should return a list")
37+
tc = test(tc, len(all_vars) > 0, "vars should contain at least one entry")
38+
# Check that vars entries are tuple of two strings
39+
for var in all_vars:
40+
tc = test(tc, type(var) == "tuple" and len(var) == 2, "Each var entry should be a tuple of length 2")
41+
tc = test(tc, type(var[0]) == "string" and type(var[1]) == "string", "Var entries should be string pairs")
42+
break
43+
44+
# Test root_dir
45+
root = ctx.std.env.root_dir()
46+
tc = test(tc, type(root) == "string", "root_dir should be a string")
47+
tc = test(tc, ctx.std.fs.is_dir(root), "root_dir should be a directory")
48+
49+
return tc
50+
51+
def test_fs(ctx: TaskContext, tc: int, temp_dir: str):
52+
# Test ctx.std.fs API in a temporary test directory
53+
54+
# Set up test directory in temp_dir
55+
test_dir = temp_dir + "/axl_fs_test"
56+
if ctx.std.fs.exists(test_dir):
57+
ctx.std.fs.remove_dir_all(test_dir)
58+
ctx.std.fs.create_dir(test_dir)
59+
tc = test(tc, ctx.std.fs.exists(test_dir), "create_dir should create the directory")
60+
tc = test(tc, ctx.std.fs.is_dir(test_dir), "created path should be a directory")
61+
tc = test(tc, not ctx.std.fs.is_file(test_dir), "created path should not be a file")
62+
63+
# Test create_dir_all
64+
deep_dir = test_dir + "/deep/a/b"
65+
ctx.std.fs.create_dir_all(deep_dir)
66+
tc = test(tc, ctx.std.fs.exists(deep_dir), "create_dir_all should create nested directories")
67+
tc = test(tc, ctx.std.fs.is_dir(deep_dir), "nested path should be a directory")
68+
69+
# Test write and read_to_string
70+
file_path = test_dir + "/test_file.txt"
71+
ctx.std.fs.write(file_path, "hello world")
72+
tc = test(tc, ctx.std.fs.exists(file_path), "write should create the file")
73+
tc = test(tc, ctx.std.fs.is_file(file_path), "written path should be a file")
74+
tc = test(tc, not ctx.std.fs.is_dir(file_path), "written path should not be a directory")
75+
content = ctx.std.fs.read_to_string(file_path)
76+
tc = test(tc, content == "hello world", "read_to_string should return the written content")
77+
78+
# Test metadata and symlink_metadata
79+
meta = ctx.std.fs.metadata(file_path)
80+
tc = test(tc, meta.is_file == True, "metadata.is_file should be True")
81+
tc = test(tc, meta.is_dir == False, "metadata.is_dir should be False")
82+
tc = test(tc, meta.is_symlink == False, "metadata.is_symlink should be False")
83+
tc = test(tc, meta.size == 11, "metadata.size should match content length")
84+
tc = test(tc, type(meta.modified) == "int" or meta.modified == None, "metadata.modified should be int or None")
85+
tc = test(tc, type(meta.accessed) == "int" or meta.accessed == None, "metadata.accessed should be int or None")
86+
tc = test(tc, type(meta.created) == "int" or meta.created == None, "metadata.created should be int or None")
87+
tc = test(tc, type(meta.readonly) == "bool", "metadata.readonly should be bool")
88+
89+
sym_meta = ctx.std.fs.symlink_metadata(file_path)
90+
tc = test(tc, sym_meta.is_file == True, "symlink_metadata.is_file should be True")
91+
tc = test(tc, sym_meta.is_dir == False, "symlink_metadata.is_dir should be False")
92+
tc = test(tc, sym_meta.is_symlink == False, "symlink_metadata.is_symlink should be False")
93+
94+
# Test copy
95+
copy_path = test_dir + "/copy.txt"
96+
ctx.std.fs.copy(file_path, copy_path)
97+
tc = test(tc, ctx.std.fs.exists(copy_path), "copy should create the new file")
98+
tc = test(tc, ctx.std.fs.read_to_string(copy_path) == "hello world", "copy should preserve content")
99+
100+
# Test rename
101+
rename_path = test_dir + "/renamed.txt"
102+
ctx.std.fs.rename(copy_path, rename_path)
103+
tc = test(tc, not ctx.std.fs.exists(copy_path), "rename should remove old path")
104+
tc = test(tc, ctx.std.fs.exists(rename_path), "rename should create new path")
105+
tc = test(tc, ctx.std.fs.read_to_string(rename_path) == "hello world", "rename should preserve content")
106+
107+
# Test hard_link
108+
hard_path = test_dir + "/hard_link.txt"
109+
ctx.std.fs.hard_link(file_path, hard_path)
110+
tc = test(tc, ctx.std.fs.exists(hard_path), "hard_link should create the link")
111+
tc = test(tc, ctx.std.fs.read_to_string(hard_path) == "hello world", "hard_link should share content")
112+
# Modify original and check link
113+
ctx.std.fs.write(file_path, "updated")
114+
tc = test(tc, ctx.std.fs.read_to_string(hard_path) == "updated", "hard_link should reflect changes")
115+
116+
# Test read_dir
117+
entries = ctx.std.fs.read_dir(test_dir)
118+
tc = test(tc, type(entries) == "list", "read_dir should return a list")
119+
tc = test(tc, len(entries) > 0, "read_dir should find entries")
120+
# Check for specific entries (basenames)
121+
found_file = False
122+
found_deep = False
123+
for entry in entries:
124+
tc = test(tc, type(entry.path) == "string", "DirEntry.path should be string")
125+
tc = test(tc, type(entry.is_file) == "bool", "DirEntry.is_file should be bool")
126+
tc = test(tc, type(entry.is_dir) == "bool", "DirEntry.is_dir should be bool")
127+
if entry.path == "test_file.txt":
128+
tc = test(tc, entry.is_file == True, "file entry should be file")
129+
tc = test(tc, entry.is_dir == False, "file entry should not be dir")
130+
found_file = True
131+
if entry.path == "deep":
132+
tc = test(tc, entry.is_dir == True, "deep entry should be dir")
133+
tc = test(tc, entry.is_file == False, "deep entry should not be file")
134+
found_deep = True
135+
tc = test(tc, found_file, "read_dir should find test_file.txt")
136+
tc = test(tc, found_deep, "read_dir should find deep")
137+
138+
# Test remove_file
139+
ctx.std.fs.remove_file(rename_path)
140+
tc = test(tc, not ctx.std.fs.exists(rename_path), "remove_file should delete the file")
141+
142+
# Test remove_dir_all
143+
ctx.std.fs.remove_dir_all(test_dir)
144+
tc = test(tc, not ctx.std.fs.exists(test_dir), "remove_dir_all should delete recursively")
145+
146+
# Test remove_dir on empty directory
147+
empty_dir = temp_dir + "/axl_empty_test"
148+
ctx.std.fs.create_dir(empty_dir)
149+
tc = test(tc, ctx.std.fs.exists(empty_dir), "create_dir should work for empty test")
150+
ctx.std.fs.remove_dir(empty_dir)
151+
tc = test(tc, not ctx.std.fs.exists(empty_dir), "remove_dir should delete empty directory")
152+
153+
# Note: Skipping remove_dir on non-empty as we can't test the fail case in this context
154+
# Note: Skipping read_link as there is no symlink creation API to test with
155+
return tc
156+
157+
def impl(ctx: TaskContext) -> int:
158+
tc = 0
159+
160+
print("Aspect CLI", ctx.std.env.aspect_cli_version())
161+
if ctx.std.env.var("ASPECT_LAUNCHER"):
162+
print("aspect-launcher detected")
163+
print("ASPECT_LAUNCHER_VERSION", ctx.std.env.var("ASPECT_LAUNCHER_VERSION"))
164+
print("ASPECT_LAUNCHER_ASPECT_CLI_METHOD", ctx.std.env.var("ASPECT_LAUNCHER_ASPECT_CLI_METHOD"))
165+
print("ASPECT_LAUNCHER_ASPECT_CLI_PATH", ctx.std.env.var("ASPECT_LAUNCHER_ASPECT_CLI_PATH"))
166+
print("ASPECT_LAUNCHER_ASPECT_CLI_URL", ctx.std.env.var("ASPECT_LAUNCHER_ASPECT_CLI_URL"))
167+
print("ASPECT_LAUNCHER_ASPECT_CLI_ORG", ctx.std.env.var("ASPECT_LAUNCHER_ASPECT_CLI_ORG"))
168+
print("ASPECT_LAUNCHER_ASPECT_CLI_REPO", ctx.std.env.var("ASPECT_LAUNCHER_ASPECT_CLI_REPO"))
169+
print("ASPECT_LAUNCHER_ASPECT_CLI_RELEASE", ctx.std.env.var("ASPECT_LAUNCHER_ASPECT_CLI_RELEASE"))
170+
print("ASPECT_LAUNCHER_ASPECT_CLI_ARTIFACT", ctx.std.env.var("ASPECT_LAUNCHER_ASPECT_CLI_ARTIFACT"))
171+
else:
172+
print("aspect-launcher not detected")
173+
174+
# Test aspect_cli_version (on CI only)
175+
if ctx.std.env.var("CI"):
176+
print("CI enviroment detected")
177+
tc = test(tc, ctx.std.env.aspect_cli_version() == "0.0.0-dev", "aspect-cli version should be 0.0.0-dev")
178+
tc = test(tc, ctx.std.env.var("ASPECT_LAUNCHER") == "true", "ASPECT_LAUNCHER should be 0.0.0-dev")
179+
tc = test(tc, ctx.std.env.var("ASPECT_LAUNCHER_VERSION") == "0.0.0-dev", "ASPECT_LAUNCHER_VERSION should be 0.0.0-dev")
180+
tc = test(tc, ctx.std.env.var("ASPECT_LAUNCHER_ASPECT_CLI_METHOD") == "local", "ASPECT_LAUNCHER_ASPECT_CLI_METHOD should be local")
181+
tc = test(tc, ctx.std.env.var("ASPECT_LAUNCHER_ASPECT_CLI_PATH") == "bazel-bin/crates/aspect-cli/aspect-cli", "ASPECT_LAUNCHER_ASPECT_CLI_PATH should be bazel-bin/crates/aspect-cli/aspect-cli")
182+
tc = test(tc, ctx.std.env.var("ASPECT_LAUNCHER_ASPECT_CLI_URL") == None, "ASPECT_LAUNCHER_ASPECT_CLI_URL should be None")
183+
tc = test(tc, ctx.std.env.var("ASPECT_LAUNCHER_ASPECT_CLI_ORG") == None, "ASPECT_LAUNCHER_ASPECT_CLI_ORG should be None")
184+
tc = test(tc, ctx.std.env.var("ASPECT_LAUNCHER_ASPECT_CLI_REPO") == None, "ASPECT_LAUNCHER_ASPECT_CLI_REPO should be None")
185+
tc = test(tc, ctx.std.env.var("ASPECT_LAUNCHER_ASPECT_CLI_RELEASE") == None, "ASPECT_LAUNCHER_ASPECT_CLI_RELEASE should be None")
186+
tc = test(tc, ctx.std.env.var("ASPECT_LAUNCHER_ASPECT_CLI_ARTIFACT") == None, "ASPECT_LAUNCHER_ASPECT_CLI_ARTIFACT should be None")
187+
else:
188+
print("local enviroment detected")
189+
190+
# Test temp_dir
191+
temp_dir = ctx.std.env.temp_dir()
192+
tc = test(tc, type(temp_dir) == "string", "temp_dir should be a string")
193+
tc = test(tc, ctx.std.fs.is_dir(temp_dir), "temp_dir should be a directory")
194+
195+
tc = test_env(ctx, tc, temp_dir)
196+
tc = test_fs(ctx, tc, temp_dir)
197+
198+
print(tc, "tests passed")
199+
return 0
200+
201+
axl = task(
202+
group = ["tests"],
203+
implementation = impl,
204+
args = {}
205+
)

0 commit comments

Comments
 (0)