Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(plugins): ensure stateless npm plugin #845

Merged
merged 2 commits into from
Sep 13, 2024
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
37 changes: 19 additions & 18 deletions craft_parts/plugins/npm_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,12 @@ def get_build_environment(self) -> Dict[str, str]:
@override
def get_pull_commands(self) -> List[str]:
"""Return a list of commands to run during the pull step."""
return []

@override
def get_build_commands(self) -> List[str]:
"""Return a list of commands to run during the build step."""
cmd = []
options = cast(NpmPluginProperties, self._options)
if options.npm_include_node:
arch = self._get_architecture()
Expand All @@ -291,25 +297,30 @@ def get_pull_commands(self) -> List[str]:
self._node_binary_path = os.path.join(
self._part_info.part_cache_dir, file_name
)
return [
cmd += [
dedent(
f"""\
if [ ! -f "{self._node_binary_path}" ]; then
mkdir -p "{self._part_info.part_cache_dir}"
curl --retry 5 -s "{checksum_uri}" -o "{self._part_info.part_cache_dir}"/SHASUMS256.txt
curl --retry 5 -s "{node_uri}" -o "{self._node_binary_path}"
fi
cd "{self._part_info.part_cache_dir}"
pushd "{self._part_info.part_cache_dir}"
sha256sum --ignore-missing --strict -c SHASUMS256.txt
popd
"""
)
]
return []

@override
def get_build_commands(self) -> List[str]:
"""Return a list of commands to run during the build step."""
cmd = [
if self._node_binary_path is not None:
cmd += [
dedent(
f"""\
tar -xzf "{self._node_binary_path}" -C "${{CRAFT_PART_INSTALL}}/" \
--no-same-owner --strip-components=1
"""
),
]
cmd += [
dedent(
"""\
NPM_VERSION="$(npm --version)"
Expand All @@ -322,14 +333,4 @@ def get_build_commands(self) -> List[str]:
"""
)
]
if self._node_binary_path is not None:
cmd.insert(
0,
dedent(
f"""\
tar -xzf "{self._node_binary_path}" -C "${{CRAFT_PART_INSTALL}}/" \
--no-same-owner --strip-components=1
"""
),
)
return cmd
11 changes: 5 additions & 6 deletions tests/unit/plugins/test_npm_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,17 +290,16 @@ def test_get_build_commands_include_node_true(
]
plugin = NpmPlugin(properties=properties, part_info=part_info)

assert plugin.get_pull_commands() == [
assert plugin.get_pull_commands() == []

assert plugin.get_build_commands() == [
f'if [ ! -f "{part_info.part_cache_dir}/node-v20.13.1-linux-x64.tar.gz" ]; then\n'
f' mkdir -p "{part_info.part_cache_dir}"\n'
f' curl --retry 5 -s "https://nodejs.org/dist/v20.13.1/SHASUMS256.txt" -o "{part_info.part_cache_dir}"/SHASUMS256.txt\n'
f' curl --retry 5 -s "https://nodejs.org/dist/v20.13.1/node-v20.13.1-linux-x64.tar.gz" -o "{part_info.part_cache_dir}/node-v20.13.1-linux-x64.tar.gz"\n'
"fi\n"
f'cd "{part_info.part_cache_dir}"\n'
"sha256sum --ignore-missing --strict -c SHASUMS256.txt\n"
]

assert plugin.get_build_commands() == [
f'pushd "{part_info.part_cache_dir}"\n'
"sha256sum --ignore-missing --strict -c SHASUMS256.txt\npopd\n",
f'tar -xzf "{part_info.part_cache_dir}/node-v20.13.1-linux-x64.tar.gz"'
' -C "${CRAFT_PART_INSTALL}/" --no-same-owner '
"--strip-components=1\n",
Expand Down
Loading