def test_project_queue(tmp_path, mock_cwd, mocker): mocker.patch('micropy.project.project.Config') path = tmp_path / 'project_path' proj = project.Project(path) # should create/load projects based on <MODULES>.PRIORITY, # not based on order added mock_parent = mocker.Mock() mock_parent.m1.return_value = mocker.Mock(PRIORITY=8) mock_parent.m2.return_value = mocker.Mock(PRIORITY=0) mock_parent.m3.return_value = mocker.Mock(PRIORITY=9) proj.add(mock_parent.m1) proj.add(mock_parent.m2) proj.add(mock_parent.m3) proj.create() # should be called in order of priority (9 being highest) mock_parent.assert_has_calls([ # adding them doesnt matter mocker.call.m1(log=mocker.ANY, parent=mocker.ANY), mocker.call.m2(log=mocker.ANY, parent=mocker.ANY), mocker.call.m3(log=mocker.ANY, parent=mocker.ANY), # create order should differ mocker.call.m3().create(), mocker.call.m1().create(), mocker.call.m2().create(), ])
def test_project_add_pkg(mocker, mock_proj_dir, shared_datadir, tmp_path, mock_pkg): """should add package to requirements""" mock_mp = mocker.patch.object(project.project, "MicroPy").return_value proj = project.Project.resolve(mock_proj_dir) proj.add_package('mock_pkg') assert proj.packages['mock_pkg'] == "*" assert proj.add_package('mock_pkg') is None # Test dev proj.add_package('another_pkg==1.0.0', dev=True) assert proj.dev_packages['another_pkg'] == '==1.0.0' # Test requirements.txt lines = proj.requirements.read_text().splitlines() assert "mock_pkg" in lines # Test Context expect_proj_stubs = mock_proj_dir / '.micropy' / "NewProject" mock_manager = mock_mp.STUBS mock_manager.resolve_subresource.return_value = [mocker.MagicMock()] mock_update = mocker.patch.object(project.project.TemplateProvider, 'update') proj = project.Project(mock_proj_dir, stub_manager=mock_manager, name="NewProject") proj.load() assert expect_proj_stubs in proj.context['paths'] mock_update.assert_called_with('vscode', proj.path, **proj.context)
def _test_project(mods="", path=None): mp = micropy_stubs() proj_path = path if path else tmp_path / "NewProject" proj = project.Project(proj_path) mods = get_module(mods, mp) for m in mods: proj.add(*m[0], **m[1]) yield proj, mp
def test_project_init(mock_mp_stubs, mock_cwd): """Test project setup""" mp = mock_mp_stubs proj_stubs = list(mp.STUBS)[:2] proj_path = mock_cwd / "ProjName" proj = project.Project(proj_path, stubs=proj_stubs) assert proj.path == mock_cwd / 'ProjName' assert proj.name == 'ProjName'
def build_project(self, mpy, path): proj_path = path / "highlevel_new_project" if proj_path.exists(): shutil.rmtree(proj_path, ignore_errors=True) proj = project.Project(proj_path) proj_stub = list(mpy.stubs)[0] proj.add(project.modules.StubsModule, mpy.stubs, stubs=[proj_stub]) proj.add(project.modules.PackagesModule, "requirements.txt") proj.add(project.modules.DevPackagesModule, "dev-requirements.txt") proj.add(project.modules.TemplatesModule, ("vscode", "pylint")) return (proj, mpy, proj_stub)
def test_project_add_stub(mocker, shared_datadir, tmp_path, mock_pkg): """should add stub to project""" mock_mp = mocker.patch.object(project.project, 'MicroPy').return_value proj_path = tmp_path / 'tmp_project' shutil.copytree((shared_datadir / 'project_test'), proj_path) # Test Loaded proj = project.Project.resolve(proj_path) proj.add_stub("mock_stub") mock_mp.STUBS.resolve_subresource.assert_called_with( [mocker.ANY, mocker.ANY, mocker.ANY, "mock_stub"], proj.data) shutil.rmtree(proj_path) shutil.copytree((shared_datadir / 'project_test'), proj_path) # Test Not loaded proj = project.Project(proj_path, stub_manager=mock_mp.STUBS) proj.add_stub("mock_stub") mock_mp.STUBS.resolve_subresource.assert_called_with( [mocker.ANY, mocker.ANY, mocker.ANY, "mock_stub"], proj.data) assert mock_mp.STUBS.resolve_subresource.call_count == 3
def test_create_project(self, mock_micropy, tmp_path, shared_datadir): mpy = mock_micropy stub_path = (shared_datadir / 'esp32_test_stub') mpy.stubs.add(stub_path) proj_path = (tmp_path / 'NewProject') self.proj = project.Project(proj_path) proj_stub = list(mpy.stubs)[0] self.proj.add(project.modules.StubsModule, mpy.stubs, stubs=[proj_stub]) self.proj.add(project.modules.PackagesModule, 'requirements.txt') self.proj.add(project.modules.DevPackagesModule, 'dev-requirements.txt') self.proj.add(project.modules.TemplatesModule, ('vscode', 'pylint')) self.proj.create() self.check_mp_data(self.proj.info_path) self.check_vscode(self.proj.path / '.vscode' / 'settings.json') # Reload micropy project and check again self.proj = mpy.resolve_project(self.proj.path) self.check_mp_data(self.proj.info_path) self.check_vscode(self.proj.path / '.vscode' / 'settings.json')
def test_project_structure(mock_mp_stubs, mock_cwd, mock_checks): """Test if project creates files""" mp = mock_mp_stubs proj_stubs = list(mp.STUBS)[:2] proj_path = mock_cwd / "ProjName" templates = ['vscode', 'pylint', 'bootstrap', 'pymakr', 'gitignore'] proj = project.Project(proj_path, templates=templates, stubs=proj_stubs, stub_manager=mp.STUBS) proj.create() templ_files = [ i.name for i in (TemplateProvider.TEMPLATE_DIR).glob("**/*") ] proj_files = sorted([i.name for i in proj.path.glob("**/*")]) expect_files = sorted([ *templ_files, *[s.path.name for s in proj.stubs], *set([s.firmware.path.name for s in proj.stubs]), '.micropy', 'micropy.json', 'requirements.txt', 'dev-requirements.txt' ]) print("Project Files:", proj_files) print("Expect:", expect_files) assert expect_files == proj_files