Skip to content

Commit

Permalink
Remove the restriction in using Go workspaces
Browse files Browse the repository at this point in the history
Since the support for workspaces was not properly implemented, we had a
strict check on the existence of a "go.work" file in a repository, which
would then trigger a failure in a request.

Now that proper support is implemented, let's remove this check.

Signed-off-by: Bruno Pimentel <[email protected]>
  • Loading branch information
brunoapimentel committed Jul 8, 2024
1 parent 86a25bb commit 3b65adf
Show file tree
Hide file tree
Showing 3 changed files with 0 additions and 115 deletions.
24 changes: 0 additions & 24 deletions cachito/workers/tasks/gomod.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,28 +43,6 @@ def _find_missing_gomod_files(bundle_dir, subpaths):
return invalid_gomod_files


def _is_workspace(repo_root: Path, subpath: str):
current_path = repo_root / subpath

while current_path != repo_root:
if (current_path / "go.work").exists():
log.warning("go.work file found at %s", current_path)
return True
current_path = current_path.parent

if (repo_root / "go.work").exists():
log.warning("go.work file found at %s", repo_root)
return True

return False


def _fail_if_bundle_dir_has_workspaces(bundle_dir: RequestBundleDir, subpaths: list[str]):
for subpath in subpaths:
if _is_workspace(bundle_dir.source_root_dir, subpath):
raise UnsupportedFeature("Go workspaces are not supported by Cachito.")


def _fail_if_parent_replacement_not_included(packages_json_data: PackagesData) -> None:
"""
Fail if any dependency replacement refers to a parent dir that isn't included in this request.
Expand Down Expand Up @@ -133,8 +111,6 @@ def fetch_gomod_source(request_id, dep_replacements=None, package_configs=None):
# Default to the root of the application source
subpaths = [os.curdir]

_fail_if_bundle_dir_has_workspaces(bundle_dir, subpaths)

invalid_gomod_files = _find_missing_gomod_files(bundle_dir, subpaths)
if invalid_gomod_files:
invalid_files_print = "; ".join(invalid_gomod_files)
Expand Down
27 changes: 0 additions & 27 deletions tests/integration/test_gomod_packages.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,33 +69,6 @@ def test_gomod_vendor_check_fail(env_name, test_env):
)


def test_gomod_workspace_check(test_env):
"""
Validate failing of gomod requests that contain workspaces.
Checks:
* The request fails with expected error message
"""
env_data = utils.load_test_data("gomod_packages.yaml")["with_workspace"]
client = utils.Client(test_env["api_url"], test_env["api_auth_type"], test_env.get("timeout"))
initial_response = client.create_new_request(
payload={
"repo": env_data["repo"],
"ref": env_data["ref"],
"pkg_managers": env_data["pkg_managers"],
},
)
completed_response = client.wait_for_complete_request(initial_response)
assert completed_response.status == 200
assert completed_response.data["state"] == "failed"
error_msg = "Go workspaces are not supported by Cachito."

assert error_msg in completed_response.data["state_reason"], (
f"#{completed_response.id}: Request failed correctly, but with unexpected message: "
f"{completed_response.data['state_reason']}. Expected message was: {error_msg}"
)


def test_gomod_with_local_replacements_in_parent_dir_missing(test_env):
"""
Test that a gomod local replacement from a parent directory includes the parent module.
Expand Down
64 changes: 0 additions & 64 deletions tests/test_workers/test_tasks/test_gomod.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import pytest

from cachito.common.packages_data import PackagesData
from cachito.common.paths import RequestBundleDir
from cachito.errors import FileAccessError, InvalidRequestData, UnsupportedFeature
from cachito.workers import tasks
from cachito.workers.tasks import gomod
Expand Down Expand Up @@ -68,7 +67,6 @@
),
)
@pytest.mark.parametrize("has_pkg_lvl_deps", (True, False))
@mock.patch("cachito.workers.tasks.gomod._fail_if_bundle_dir_has_workspaces")
@mock.patch("cachito.workers.tasks.gomod.RequestBundleDir")
@mock.patch("cachito.workers.tasks.gomod.update_request_env_vars")
@mock.patch("cachito.workers.tasks.gomod.set_request_state")
Expand All @@ -80,7 +78,6 @@ def test_fetch_gomod_source(
mock_set_request_state,
mock_update_request_env_vars,
mock_bundle_dir,
mock_fail_workspaces,
dep_replacements,
expect_state_update,
pkg_config,
Expand Down Expand Up @@ -268,15 +265,13 @@ def directory_present(*args, **kwargs):
),
),
)
@mock.patch("cachito.workers.tasks.gomod._fail_if_bundle_dir_has_workspaces")
@mock.patch("cachito.workers.tasks.gomod.get_worker_config")
@mock.patch("cachito.workers.tasks.gomod.RequestBundleDir")
@mock.patch("cachito.workers.tasks.gomod.resolve_gomod")
def test_fetch_gomod_source_no_go_mod_file(
mock_resolve_gomod,
mock_bundle_dir,
mock_gwc,
mock_fail_workspaces,
ignore_missing_gomod_file,
exception_expected,
pkg_config,
Expand Down Expand Up @@ -308,65 +303,6 @@ def directory_present(*args, **kwargs):
mock_resolve_gomod.assert_not_called()


@pytest.mark.parametrize(
"module_name, package_name, module_subpath, expect_subpath",
[
("github.com/foo", "github.com/foo", ".", "."),
("github.com/foo", "github.com/foo", "bar", "bar"),
("github.com/foo", "github.com/foo/bar", ".", "bar"),
("github.com/foo", "github.com/foo/bar", "src", "src/bar"),
],
)
def test_package_subpath(module_name, package_name, module_subpath, expect_subpath):
assert gomod._package_subpath(module_name, package_name, module_subpath) == expect_subpath


@pytest.mark.parametrize(
"repo, subpath, expected_result",
[
("repo", "workspace", True),
("repo", "workspace/mod_a", True),
("repo", "workspace/mod_b", True),
("repo", "nonworspace", False),
("repo", "nonworspace/mod_c", False),
("repo", "randompath", False),
("anotherrepo", ".", True),
],
)
def test_is_workspace(repo, subpath, expected_result, tmpdir):
tmpdir.mkdir("repo")
tmpdir.mkdir("repo/workspace")
tmpdir.mkdir("repo/workspace/mod_a")
tmpdir.mkdir("repo/workspace/mod_b")
tmpdir.mkdir("repo/nonworspace")
tmpdir.mkdir("repo/nonworspace/mod_c")
tmpdir.mkdir("anotherrepo")

Path(tmpdir / "repo/workspace" / "go.work").touch()
Path(tmpdir / "anotherrepo" / "go.work").touch()

repo_root = Path(tmpdir / repo)
result = gomod._is_workspace(repo_root, subpath)

assert result == expected_result


@pytest.mark.parametrize("add_go_work_file", [True, False])
def test_fail_if_bundle_dir_has_workspaces(add_go_work_file, tmpdir):
tmpdir.mkdir("temp")
tmpdir.mkdir("temp/1")
tmpdir.mkdir("temp/1/app")

bundle_dir = RequestBundleDir(1, tmpdir)

if add_go_work_file:
Path(bundle_dir.source_root_dir / "go.work").touch()
with pytest.raises(UnsupportedFeature):
gomod._fail_if_bundle_dir_has_workspaces(bundle_dir, ["."])
else:
gomod._fail_if_bundle_dir_has_workspaces(bundle_dir, ["."])


@pytest.mark.parametrize(
"packages",
[
Expand Down

0 comments on commit 3b65adf

Please sign in to comment.