def test_build__auto_init_with_nonexisting_build_dir(self, tmpdir, capsys): ctx = MockContext() project_dir = Path(str(tmpdir)) project_build_dir = project_dir/"build" build_config = BuildConfig(cmake_generator="ninja") cmake_project1 = CMakeProject(ctx, project_dir, project_build_dir, build_config) assert not project_build_dir.isdir() # -- STEP: First cmake_project.build => AUTO CMAKE-INIT: project_build_dir with cd(project_dir): assert not project_build_dir.exists() cmake_project1.build() # -- POSTCONDITIONS: expected_commands = [ "cmake -G Ninja ..", "cmake --build .", ] assert ctx.commands == expected_commands cmake_build_filename = project_build_dir / CMakeProjectPersistConfig.FILE_BASENAME captured = capsys.readouterr() assert_cmake_project_used_init_using_captured(cmake_project1, captured, cmake_generator="ninja") # assert "CMAKE-INIT: build (using cmake.generator=ninja)" in captured.out assert "CMAKE-BUILD: build" in captured.out assert project_build_dir.exists() assert cmake_build_filename.exists()
def test_init__init_without_build_dir(self, tmpdir): ctx = MockContext() project_dir = Path(str(tmpdir)) project_build_dir = project_dir/"build" build_config = BuildConfig(cmake_generator="NINJA") cmake_project = CMakeProject(ctx, project_dir, project_build_dir, build_config) assert not project_build_dir.isdir() with cd(project_dir): cmake_project.init() assert ctx.last_command == "cmake -G NINJA .."
def test_init__skip_same_data(self, tmpdir, capsys): ctx = MockContext() project_dir = Path(str(tmpdir)) project_build_dir = project_dir/"build" build_config = BuildConfig(cmake_generator="NINJA") cmake_project1 = CMakeProject(ctx, project_dir, project_build_dir, build_config) assert not project_build_dir.isdir() # -- STEP 1: First cmake_project.init with cd(project_dir): cmake_project1.init() # -- POSTCONDITIONS: cmake_build_filename = project_build_dir / CMakeProjectPersistConfig.FILE_BASENAME assert project_build_dir.exists(), "ENSURE: project_build_dir exists" assert cmake_build_filename.exists() captured = capsys.readouterr() assert_cmake_project_used_init_using_captured(cmake_project1, captured, cmake_generator="NINJA") # -- STEP 2: Second cmake_project.init => SKIPPED with cd(project_dir): ctx.clear() cmake_project2 = CMakeProject(ctx, project_dir.relpath(), project_build_dir.relpath(), build_config) cmake_project2.init() captured = capsys.readouterr() assert ctx.last_command is None assert_cmake_project_skipped_reinit_using_captured(cmake_project1, captured)
def make_cmake_project(ctx, project_dir, build_config=None, strict=False, **kwargs): if not ctx.config.build_configs_map: # -- LAZY-INIT: Build build_configs_map once from build_configs list. build_configs_map = make_build_configs_map(ctx.config.build_configs) ctx.config.build_configs_map = build_configs_map # MAYBE-MORE: # CMakeBuildConfigNormalizer.normalize(ctx.config) cmake_generator = kwargs.pop("generator", None) build_config_default = ctx.config.build_config or BUILD_CONFIG_DEFAULT build_config = build_config or build_config_default require_build_config_is_valid_or_none(ctx, build_config, strict=strict) # -- CREATE CMAKE PROJECT: # HINT: Detect SPECIAL CASES with FAULT SYNDROMES first. project_dir = Path(project_dir) cmake_list_file = project_dir / "CMakeLists.txt" if not project_dir.isdir(): cmake_project = CMakeProjectWithoutProjectDirectory(project_dir) elif not cmake_list_file.isfile(): cmake_project = CMakeProjectWithoutCMakeListsFile(project_dir) else: # -- NORMAL CASE: build_config_obj = make_build_config(ctx, build_config) cmake_project = CMakeProject(ctx, project_dir, build_config=build_config_obj, cmake_generator=cmake_generator) show_cmake_project_ignored_args(kwargs) return cmake_project
def test_init__skips_reinit_with_existing_build_dir_and_generator_none(self, tmpdir, capsys): ctx = MockContext() project_dir = Path(str(tmpdir)) project_build_dir = project_dir/"build" build_config = BuildConfig(cmake_generator="ninja") assert not project_build_dir.isdir() # -- STEP 1: First cmake_project.init with cd(project_dir): cmake_project1 = CMakeProject(ctx, project_dir, project_build_dir, build_config) cmake_project1.init() # -- POSTCONDITIONS: cmake_build_filename = project_build_dir / CMakeProjectPersistConfig.FILE_BASENAME assert project_build_dir.exists(), "ENSURE: project_build_dir exists" assert cmake_build_filename.exists() captured = capsys.readouterr() assert_cmake_project_used_init_using_captured(cmake_project1, captured, cmake_generator="ninja") # assert "CMAKE-INIT: build (using cmake.generator=ninja)" in captured.out assert ctx.last_command == "cmake -G Ninja .." # -- STEP 2: Second cmake_project.build => SKIP-REINIT. # REASON: # * Inherit stored.cmake_generator (if it is not overridden). # * Keep stored.cmake_generator until explicit reinit. ctx.clear() with cd(project_dir): build_config.cmake_generator = "OTHER" # -- ONLY DEFAULT-VALUE cmake_project2 = CMakeProject(ctx, project_dir, project_build_dir, build_config, cmake_generator=None) assert not cmake_project2.needs_reinit() cmake_project2.init() # -- POSTCONDITIONS: expected_commands = [] assert ctx.commands == expected_commands captured = capsys.readouterr() assert_cmake_project_skipped_reinit_using_captured(cmake_project2, captured, cmake_generator="ninja")
def test_build__performs_reinit_with_existing_build_dir_and_other_cmake_generator(self, tmpdir, capsys): ctx = MockContext() project_dir = Path(str(tmpdir)) project_build_dir = project_dir/"build" build_config = BuildConfig(cmake_generator="ninja") assert not project_build_dir.isdir() # -- STEP 1: First cmake_project.init with cd(project_dir): cmake_project1 = CMakeProject(ctx, project_dir, project_build_dir, build_config) cmake_project1.init() # -- POSTCONDITIONS: cmake_build_filename = project_build_dir / CMakeProjectPersistConfig.FILE_BASENAME assert project_build_dir.exists(), "ENSURE: project_build_dir exists" assert cmake_build_filename.exists() captured = capsys.readouterr() assert_cmake_project_used_init_using_captured(cmake_project1, captured, cmake_generator="ninja") # assert "CMAKE-INIT: build (using cmake.generator=ninja)" in captured.out assert ctx.last_command == "cmake -G Ninja .." # -- STEP 2: Second cmake_project.build => REINIT. ctx.clear() with cd(project_dir): build_config.cmake_generator = "OTHER" # ONLY-DEFAULT cmake_project2 = CMakeProject(ctx, project_dir, project_build_dir, build_config, cmake_generator="make") assert cmake_project2.needs_reinit() cmake_project2.build() # -- POSTCONDITIONS: expected_commands = [ 'cmake -G "Unix Makefiles" ..', "cmake --build ." ] assert ctx.commands == expected_commands captured = capsys.readouterr() assert_cmake_project_used_reinit_using_captured(cmake_project2, captured, cmake_generator="make")
def make_newborn(tmpdir, cmake_generator=None, cmake_build_type=None): """Create NEW-BORN CMake project without build-directory.""" cmake_generator = cmake_generator or DEFAULT_CMAKE_GENERATOR cmake_build_type = cmake_build_type or DEFAULT_CMAKE_BUILD_TYPE ctx = MockContext() project_dir = Path(str(tmpdir)) project_build_dir = project_dir / "build" build_config = BuildConfig(cmake_generator=cmake_generator, cmake_build_type=cmake_build_type) assert not project_build_dir.isdir() # -- STEP 1: First cmake_project.init with cd(project_dir): cmake_project = CMakeProject(ctx, project_dir, project_build_dir, build_config) return cmake_project
def test_init__performs_reinit_with_other_cmake_generator(self, tmpdir, capsys): ctx = MockContext() project_dir = Path(str(tmpdir)).abspath() project_build_dir = project_dir/"build" build_config = BuildConfig(cmake_generator="make") cmake_project1 = CMakeProject(ctx, project_dir, project_build_dir, build_config) assert not project_build_dir.isdir() # -- STEP 1: First cmake_project.init with cd(project_dir): cmake_project1.init() # -- POSTCONDITIONS: cmake_build_filename = project_build_dir / CMakeProjectPersistConfig.FILE_BASENAME assert project_build_dir.exists(), "ENSURE: project_build_dir exists" assert cmake_build_filename.exists() captured = capsys.readouterr() assert_cmake_project_used_init_using_captured(cmake_project1, captured, cmake_generator="make") # assert "CMAKE-INIT: build (using cmake.generator=make)" in captured.out assert ctx.last_command == 'cmake -G "Unix Makefiles" ..' # -- STEP 2: Second cmake_project.init => REINIT: Other cmake_generator is used. with cd(project_dir): ctx.clear() # build_config.cmake_generator = "ninja" cmake_project2 = CMakeProject(ctx, project_dir.relpath(), project_build_dir.relpath(), build_config, cmake_generator="ninja") assert cmake_project2.needs_reinit(), "ENSURE: Need for reinit" cmake_project2.init() captured = capsys.readouterr() assert_cmake_project_used_reinit_using_captured(cmake_project2, captured, cmake_generator="ninja") # assert "CMAKE-INIT: build (NEEDS-REINIT)" in captured.out # assert "CMAKE-INIT: build (using cmake.generator=ninja)" in captured.out assert ctx.last_command == "cmake -G Ninja .."
def test_init__when_build_dir_exists_with_other_persistent_schema(self, tmpdir, capsys): ctx = MockContext() project_dir = Path(str(tmpdir)) project_build_dir = project_dir/"build" build_config = BuildConfig(cmake_generator="ninja", cmake_build_type="debug") assert not project_build_dir.isdir() # -- STEP 1: First cmake_project.init with cd(project_dir): cmake_project1 = CMakeProject(ctx, project_dir, project_build_dir, build_config) cmake_project1.init() cmake_build_filename = project_build_dir / CMakeProjectPersistConfig.FILE_BASENAME assert project_build_dir.exists(), "ENSURE: project_build_dir exists" assert cmake_build_filename.exists() # -- STEP: Fake cmake-build init with other persistent data schema. # HINT: May occur when cmake-build is updated, but project_build_dir still exists. with open(cmake_build_filename, "w") as f: f.write("""{ "other": 123, "cmake_generator": "ninja" }""") # -- STEP 2: Second try to cmake_project.init() # ENSURE: No failure / AssertionError occurs with cd(project_dir): cmake_project2 = CMakeProject(ctx, project_dir, project_build_dir, build_config) assert cmake_project2.initialized assert not cmake_project2.needs_reinit() assert cmake_project2.needs_update() cmake_project2.init() assert not cmake_project2.needs_reinit() assert not cmake_project2.needs_update() captured = capsys.readouterr() assert_cmake_project_needed_update_using_captured(cmake_project2, captured, cmake_generator="ninja")