Skip to content

Commit 7d5a1d0

Browse files
committed
documentation and cleanup
1 parent d141caf commit 7d5a1d0

File tree

6 files changed

+125
-16
lines changed

6 files changed

+125
-16
lines changed

aws_lambda_builders/workflows/nodejs_npm/actions.py

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,44 @@
1111

1212
class NodejsNpmPackAction(BaseAction):
1313

14+
"""
15+
A Lambda Builder Action that packages a Node.js package using NPM to extract the source and remove test resources
16+
"""
17+
1418
NAME = 'NpmPack'
1519
DESCRIPTION = "Packaging source using NPM"
1620
PURPOSE = Purpose.COPY_SOURCE
1721

1822
def __init__(self, artifacts_dir, scratch_dir, manifest_path, osutils, subprocess_npm):
23+
"""
24+
:type artifacts_dir: str
25+
:param artifacts_dir: an existing (writable) directory where to store the output.
26+
Note that the actual result will be in the 'package' subdirectory here.
27+
28+
:type scratch_dir: str
29+
:param scratch_dir: an existing (writable) directory for temporary files
30+
31+
:type manifest_path: str
32+
:param manifest_path: path to package.json of an NPM project with the source to pack
33+
34+
:type osutils: aws_lambda_builders.workflows.nodejs_npm.utils.OSUtils
35+
:param osutils: An instance of OS Utilities for file manipulation
36+
37+
:type subprocess_npm: aws_lambda_builders.workflows.nodejs_npm.npm.SubprocessNpm
38+
:param subprocess_npm: An instance of the NPM process wrapper
39+
"""
1940
self.artifacts_dir = artifacts_dir
2041
self.manifest_path = manifest_path
2142
self.scratch_dir = scratch_dir
2243
self.osutils = osutils
2344
self.subprocess_npm = subprocess_npm
2445

2546
def execute(self):
47+
"""
48+
Runs the action.
49+
50+
:raises lambda_builders.actions.ActionFailedError: when NPM packaging fails
51+
"""
2652
try:
2753
package_path = "file:{}".format(self.osutils.abspath(self.osutils.dirname(self.manifest_path)))
2854

@@ -44,19 +70,36 @@ def execute(self):
4470

4571
class NodejsNpmInstallAction(BaseAction):
4672

73+
"""
74+
A Lambda Builder Action that installs NPM project dependencies
75+
"""
76+
4777
NAME = 'NpmInstall'
4878
DESCRIPTION = "Installing dependencies from NPM"
4979
PURPOSE = Purpose.RESOLVE_DEPENDENCIES
5080

51-
def __init__(self, artifacts_dir, manifest_path, osutils, subprocess_npm):
81+
def __init__(self, artifacts_dir, subprocess_npm):
82+
"""
83+
:type artifacts_dir: str
84+
:param artifacts_dir: an existing (writable) directory with project source files.
85+
Dependencies will be installed in this directory.
86+
87+
:type subprocess_npm: aws_lambda_builders.workflows.nodejs_npm.npm.SubprocessNpm
88+
:param subprocess_npm: An instance of the NPM process wrapper
89+
"""
90+
5291
self.artifacts_dir = artifacts_dir
53-
self.manifest_path = manifest_path
54-
self.osutils = osutils
5592
self.subprocess_npm = subprocess_npm
5693

5794
def execute(self):
95+
"""
96+
Runs the action.
97+
98+
:raises lambda_builders.actions.ActionFailedError: when NPM execution fails
99+
"""
100+
58101
try:
59-
LOG.debug("NODEJS installing in: %s from: %s", self.artifacts_dir, self.manifest_path)
102+
LOG.debug("NODEJS installing in: %s", self.artifacts_dir)
60103

61104
self.subprocess_npm.run(
62105
['install', '-q', '--no-audit', '--no-save', '--production'],

aws_lambda_builders/workflows/nodejs_npm/npm.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@
88

99

1010
class NpmExecutionError(Exception):
11+
12+
"""
13+
Exception raised in case NPM execution fails.
14+
It will pass on the standard error output from the NPM console.
15+
"""
16+
1117
MESSAGE = "NPM Failed: {message}"
1218

1319
def __init__(self, **kwargs):
@@ -16,7 +22,20 @@ def __init__(self, **kwargs):
1622

1723
class SubprocessNpm(object):
1824

25+
"""
26+
Wrapper around the NPM command line utility, making it
27+
easy to consume execution results.
28+
"""
29+
1930
def __init__(self, osutils, npm_exe=None):
31+
"""
32+
:type osutils: aws_lambda_builders.workflows.nodejs_npm.utils.OSUtils
33+
:param osutils: An instance of OS Utilities for file manipulation
34+
35+
:type npm_exe: str
36+
:param npm_exe: Path to the NPM binary. If not set,
37+
the default executable path npm will be used
38+
"""
2039
self.osutils = osutils
2140

2241
if npm_exe is None:
@@ -26,6 +45,25 @@ def __init__(self, osutils, npm_exe=None):
2645

2746
def run(self, args, cwd=None):
2847

48+
"""
49+
Runs the action.
50+
51+
:type args: list
52+
:param args: Command line arguments to pass to NPM
53+
54+
:type cwd: str
55+
:param cwd: Directory where to execute the command (defaults to current dir)
56+
57+
:rtype: str
58+
:return: text of the standard output from the command
59+
60+
:raises aws_lambda_builders.workflows.nodejs_npm.npm.NpmExecutionError:
61+
when the command executes with a non-zero return code. The exception will
62+
contain the text of the standard error output from the command.
63+
64+
:raises ValueError: if arguments are not provided, or not a list
65+
"""
66+
2967
if not isinstance(args, list):
3068
raise ValueError('args must be a list')
3169

aws_lambda_builders/workflows/nodejs_npm/utils.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@
99

1010
class OSUtils(object):
1111

12+
"""
13+
Wrapper around file system functions, to make it easy to
14+
unit test actions in memory
15+
"""
16+
1217
def extract_tarfile(self, tarfile_path, unpack_dir):
1318
with tarfile.open(tarfile_path, 'r:*') as tar:
1419
tar.extractall(unpack_dir)

aws_lambda_builders/workflows/nodejs_npm/workflow.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212

1313
class NodejsNpmWorkflow(BaseWorkflow):
1414

15+
"""
16+
A Lambda builder workflow that knows how to pack
17+
NodeJS projects using NPM.
18+
"""
1519
NAME = "NodejsNpmBuilder"
1620

1721
CAPABILITY = Capability(language="nodejs",
@@ -51,8 +55,6 @@ def __init__(self,
5155
subprocess_npm=subprocess_npm)
5256

5357
npm_install = NodejsNpmInstallAction(artifacts_dir,
54-
manifest_path,
55-
osutils=osutils,
5658
subprocess_npm=subprocess_npm)
5759
self.actions = [
5860
npm_pack,

tests/unit/workflows/nodejs_npm/test_actions.py

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,11 @@ def test_raises_action_failed_when_npm_fails(self, OSUtilMock, SubprocessNpmMock
5353

5454
class TestNodejsNpmInstallAction(TestCase):
5555

56-
@patch("aws_lambda_builders.workflows.nodejs_npm.utils.OSUtils")
5756
@patch("aws_lambda_builders.workflows.nodejs_npm.npm.SubprocessNpm")
58-
def test_tars_and_unpacks_npm_project(self, OSUtilMock, SubprocessNpmMock):
59-
osutils = OSUtilMock.return_value
57+
def test_tars_and_unpacks_npm_project(self, SubprocessNpmMock):
6058
subprocess_npm = SubprocessNpmMock.return_value
6159

62-
action = NodejsNpmInstallAction("artifacts", "manifest",
63-
osutils=osutils,
60+
action = NodejsNpmInstallAction("artifacts",
6461
subprocess_npm=subprocess_npm)
6562

6663
action.execute()
@@ -69,18 +66,14 @@ def test_tars_and_unpacks_npm_project(self, OSUtilMock, SubprocessNpmMock):
6966

7067
subprocess_npm.run.assert_called_with(expected_args, cwd='artifacts')
7168

72-
@patch("aws_lambda_builders.workflows.nodejs_npm.utils.OSUtils")
7369
@patch("aws_lambda_builders.workflows.nodejs_npm.npm.SubprocessNpm")
74-
def test_raises_action_failed_when_npm_fails(self, OSUtilMock, SubprocessNpmMock):
75-
osutils = OSUtilMock.return_value
70+
def test_raises_action_failed_when_npm_fails(self, SubprocessNpmMock):
7671
subprocess_npm = SubprocessNpmMock.return_value
7772

7873
builder_instance = SubprocessNpmMock.return_value
7974
builder_instance.run.side_effect = NpmExecutionError(message="boom!")
8075

8176
action = NodejsNpmInstallAction("artifacts",
82-
"manifest",
83-
osutils=osutils,
8477
subprocess_npm=subprocess_npm)
8578

8679
with self.assertRaises(ActionFailedError) as raised:
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
2+
from unittest import TestCase
3+
4+
from aws_lambda_builders.workflows.nodejs_npm.workflow import NodejsNpmWorkflow
5+
6+
from aws_lambda_builders.workflows.nodejs_npm.actions import NodejsNpmPackAction, NodejsNpmInstallAction
7+
8+
from aws_lambda_builders.actions import CopySourceAction
9+
10+
11+
class TestNodejsNpmWorkflow(TestCase):
12+
13+
"""
14+
the workflow requires an external utility (npm) to run, so it is extensively tested in integration tests.
15+
this is just a quick wiring test to provide fast feedback if things are badly broken
16+
"""
17+
18+
def test_workflow_sets_up_npm_actions(self):
19+
20+
workflow = NodejsNpmWorkflow("source", "artifacts", "scratch_dir", "manifest")
21+
22+
assert len(workflow.actions) == 3
23+
24+
assert isinstance(workflow.actions[0], NodejsNpmPackAction)
25+
26+
assert isinstance(workflow.actions[1], CopySourceAction)
27+
28+
assert isinstance(workflow.actions[2], NodejsNpmInstallAction)

0 commit comments

Comments
 (0)