Skip to content

Commit

Permalink
Add regression test for microtvm_api_server not failing
Browse files Browse the repository at this point in the history
  • Loading branch information
guberti committed Aug 27, 2021
1 parent 69b01ac commit 3a36816
Showing 1 changed file with 42 additions and 2 deletions.
44 changes: 42 additions & 2 deletions tests/micro/arduino/test_arduino_workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@
import pathlib
import shutil
import sys
import tempfile

import pytest
import tvm
from tvm import micro, relay

import conftest
import tvm
from tvm import micro, relay
from tvm.micro.project_api.server import ServerError

"""
This unit test simulates a simple user workflow, where we:
Expand All @@ -49,6 +51,30 @@ def project_dir(workspace_dir):
return workspace_dir / "project"


# Saves the Arduino project's state, runs the test, then resets it
@pytest.fixture(scope="function")
def does_not_affect_state(project_dir):
with tempfile.TemporaryDirectory() as temp_dir:
prev_project_state = pathlib.Path(temp_dir)
shutil.copytree(project_dir, prev_project_state / "project")
yield

# We can't delete project_dir or it'll mess up the Arduino CLI working directory
# Instead, delete everything in project_dir, and then copy over the files
for item in project_dir.iterdir():
if item.is_dir():
shutil.rmtree(item)
else:
item.unlink() # Delete file
# Once we upgrade to Python 3.7, this can be replaced with
# shutil.copytree(dirs_exist_ok=True)
for item in (prev_project_state / "project").iterdir():
if item.is_dir():
shutil.copytree(item, project_dir / item.name)
else:
shutil.copy2(item, project_dir)


def _generate_project(arduino_board, arduino_cli_cmd, workspace_dir, mod, build_config):
return tvm.micro.generate_project(
str(conftest.TEMPLATE_PROJECT_DIR),
Expand Down Expand Up @@ -135,6 +161,20 @@ def test_import_rerouting(project_dir, project):
assert "include/tvm/runtime/crt/platform.h" in c_backend_api_c


@pytest.mark.usefixtures("does_not_affect_state")
def test_blank_project_compiles(project):
project.build()


# Add a bug (an extra curly brace) and make sure the project doesn't compile
@pytest.mark.usefixtures("does_not_affect_state")
def test_bugged_project_compile_fails(project_dir, project):
with open(project_dir / "project.ino", "a") as main_file:
main_file.write("}\n")
with pytest.raises(ServerError):
project.build()


# Build on top of the generated project by replacing the
# top-level .ino fileand adding data input files, much
# like a user would
Expand Down

0 comments on commit 3a36816

Please sign in to comment.