Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/cq_cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,11 @@ def get_script_from_infile(infile, outfile, errfile):
script_str = infile
else:
with open(infile, "r") as file:
script_str = file.read()
# prepend an assignment for the __file__ variable so the model
# script knows its path and can potentially load resources relative
# to that path.
script_str = f"__file__ = '{os.path.abspath(infile)}'\n"
script_str += file.read()

return script_str

Expand Down
68 changes: 46 additions & 22 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,21 +291,20 @@ def test_parameter_analysis():

# Grab the JSON output from cq-cli
jsn = json.loads(out.decode())

# Check to make sure the first parameter was handled properly
assert jsn[0]["type"] == "number"
assert jsn[0]["name"] == "width"
assert jsn[0]["initial"] == 1

# Check to make sure the second parameter was handled properly
assert jsn[1]["type"] == "string"
assert jsn[1]["name"] == "tag_name"
assert jsn[1]["initial"] == "cube"

# Check to make sure the third parameter was handled properly
assert jsn[2]["type"] == "boolean"
assert jsn[2]["name"] == "centered"
assert jsn[2]["initial"] == True
params_by_name = helpers.params_list_to_dict(jsn)

# Check to make sure the parameters were handled properly
assert params_by_name["width"] == {"name": "width", "type": "number", "initial": 1}
assert params_by_name["tag_name"] == {
"name": "tag_name",
"type": "string",
"initial": "cube",
}
assert params_by_name["centered"] == {
"name": "centered",
"type": "boolean",
"initial": True,
}


def test_parameter_file_input_output():
Expand Down Expand Up @@ -348,10 +347,10 @@ def test_parameter_file_input_output():
# Modify the parameters file
with open(temp_file, "r") as file:
json_str = file.read()
json_dict = json.loads(json_str)
json_dict[0]["initial"] = 10
json_list = json.loads(json_str)
json_list[1]["initial"] = 10
with open(temp_file, "w") as file:
file.writelines(json.dumps(json_dict))
file.writelines(json.dumps(json_list))

# Run the command with the new parameters
command3 = [
Expand Down Expand Up @@ -414,10 +413,11 @@ def test_params_stl_output():
# Make sure that the customizer.json file exists and has what we expect in it
with open(customizer_file_path, "r") as file2:
json_str = file2.read()
json_dict = json.loads(json_str)
assert json_dict[0]["initial"] == 1
assert json_dict[1]["initial"] == "cube"
assert json_dict[2]["initial"] == True
json_list = json.loads(json_str)
params = helpers.params_list_to_dict(json_list)
assert params["width"]["initial"] == 1
assert params["tag_name"]["initial"] == "cube"
assert params["centered"]["initial"] == True

# Write an STL using the default parameters so that we can compare it to what was generated with customized parameters
command = [
Expand Down Expand Up @@ -544,3 +544,27 @@ def test_multiple_outfiles():
]
out, err, exitcode = helpers.cli_call(command)
assert exitcode == 0


def test_file_variable_is_set():
"""
Tests that cq-cli sets the __file__ variable for the model script.
"""
test_file = helpers.get_test_file_location("file_var.py")

temp_dir = tempfile.gettempdir()
out_path = os.path.join(temp_dir, "temp_test_file_variable.stl")

command = [
"python",
"src/cq_cli/main.py",
"--codec",
"stl",
"--infile",
test_file,
"--outfile",
out_path,
]
out, err, exitcode = helpers.cli_call(command)
assert exitcode == 0
assert "__file__=" in out.decode()
12 changes: 11 additions & 1 deletion tests/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@ def cli_call(command):
"""
Makes the operating system process calls to test the CLI properly.
"""
proc = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE,)
proc = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = proc.communicate()
return out, err, proc.returncode


def params_list_to_dict(param_list):
"""
Converts a list of params into a dictionary of those params keyed by name.
"""
d = {}
for entry in param_list:
d[entry["name"]] = entry
return d
11 changes: 11 additions & 0 deletions tests/testdata/file_var.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import cadquery as cq

# print info about the __file__ variable to stdout so the test can check it
if "__file__" in locals():
print(f"__file__={__file__}")
else:
print("__FILE__ not set")

# render a simple shape so the cq-cli invocation succeeds
b = cq.Workplane("XY").rect(10, 10).extrude(10)
show_object(b)
Loading