Skip to content

Commit

Permalink
nodejs pluging: support for type str bin entries
Browse files Browse the repository at this point in the history
The plugin was only capable of parsing bin entries inside package.json
of type dict, this adds support for str entries, taking into account
scoped package names.

LP: #1817553
Signed-off-by: Sergio Schvezov <sergio.schvezov@canonical.com>
  • Loading branch information
sergiusens committed Mar 12, 2019
1 parent 47141bf commit fb2b29c
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 14 deletions.
32 changes: 19 additions & 13 deletions snapcraft/plugins/nodejs.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,29 +290,35 @@ def get_manifest(self):


def _create_bins(package_json, directory):
binaries = package_json.get("bin")
if not binaries:
bin_entry = package_json.get("bin")
if not bin_entry:
return

bin_dir = os.path.join(directory, "bin")
os.makedirs(bin_dir, exist_ok=True)

if type(binaries) == dict:
for bin_name, bin_path in binaries.items():
target = os.path.join(bin_dir, bin_name)
# The binary might be already created from upstream sources.
if os.path.exists(os.path.join(target)):
continue
source = os.path.join("..", bin_path)
os.symlink(source, target)
# Make it executable
os.chmod(os.path.realpath(target), 0o755)
if type(bin_entry) == dict:
binaries = bin_entry
elif type(bin_entry) == str:
# Support for scoped names of the form of @org/name
name = package_json["name"]
binaries = {name[name.find("/") + 1 :]: bin_entry}
else:
raise errors.SnapcraftEnvironmentError(
"The plugin is not prepared to handle bin entries of "
"type {!r}".format(type(binaries))
"type {!r}".format(type(bin_entry))
)

for bin_name, bin_path in binaries.items():
target = os.path.join(bin_dir, bin_name)
# The binary might be already created from upstream sources.
if os.path.exists(os.path.join(target)):
continue
source = os.path.join("..", bin_path)
os.symlink(source, target)
# Make it executable
os.chmod(os.path.realpath(target), 0o755)


def _get_nodejs_base(node_engine, machine):
if machine not in _NODEJS_ARCHES:
Expand Down
54 changes: 53 additions & 1 deletion tests/unit/plugins/test_nodejs.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,11 @@ class Options:
self.useFixture(fixture_setup.CleanEnvironment())

def create_assets(
self, plugin, package_name="test-nodejs", skip_package_json=False
self,
plugin,
package_name="test-nodejs",
single_bin=False,
skip_package_json=False,
):
for directory in (plugin.sourcedir, plugin.builddir):
os.makedirs(directory)
Expand Down Expand Up @@ -555,6 +559,54 @@ def test_get_manifest_with_yarn_lock_file(self):
self.assertThat(plugin.get_manifest(), Equals(expected_manifest))


class NodeBinTest(unit.TestCase):
scenarios = [
(
"dict",
dict(
package_json=dict(
name="package-foo",
bin=dict(run1="bin0/run1bin", run2="bin1/run2bin"),
),
expected_bins=["run1", "run2"],
),
),
(
"single",
dict(
package_json=dict(name="package-foo", bin="bin0/run1bin"),
expected_bins=["package-foo"],
),
),
(
"single, scoped package",
dict(
package_json=dict(name="@org/package-foo", bin="bin1/run1bin"),
expected_bins=["package-foo"],
),
),
]

def setUp(self):
super().setUp()

if type(self.package_json["bin"]) == dict:
binaries = self.package_json["bin"].values()
else:
binaries = [self.package_json["bin"]]

for binary in binaries:
os.makedirs(os.path.dirname(binary), exist_ok=True)
open(binary, "w").close()

def test_bins(self):
nodejs._create_bins(self.package_json, ".")
binary_paths = (os.path.join("bin", b) for b in self.expected_bins)

for binary in binary_paths:
self.assertThat(binary, FileExists())


class NodeReleaseTest(unit.TestCase):
scenarios = [
(
Expand Down

0 comments on commit fb2b29c

Please sign in to comment.