def test_init(self, mock_warn, runway_context, tmp_path): """Test init and the attributes set in init.""" # pylint: disable=no-member options = { 'environments': True, # can only be True of a falsy value (but not dict) 'options': {'test_option': 'option_value'}, 'parameters': {'test_parameter': 'parameter_value'}, 'extra': 'something' } obj = RunwayModuleNpm(context=runway_context, path=str(tmp_path), options=options.copy()) obj.check_for_npm.assert_called_once() assert obj.context == runway_context assert obj.environments == options['environments'] assert obj.extra == options['extra'] assert obj.options == options['options'] assert obj.parameters == options['parameters'] assert isinstance(obj.path, Path) assert str(obj.path) == str(tmp_path) mock_warn.assert_called_once_with(runway_context.env_vars) obj = RunwayModuleNpm(context=runway_context, path=tmp_path) assert not obj.environments assert not obj.options assert not obj.parameters assert obj.path == tmp_path
def test_log_npm_command(self, mock_log, caplog, patch_module_npm, runway_context): """Test log_npm_command.""" mock_log.return_value = "success" obj = RunwayModuleNpm(context=runway_context, path="./tests", options={}) caplog.set_level(logging.DEBUG, logger="runway") assert not obj.log_npm_command(["npm", "test"]) mock_log.assert_called_once_with(["npm", "test"]) assert ["node command: success"] == caplog.messages
def test_log_npm_command(self, mock_log, caplog, patch_module_npm, runway_context): """Test log_npm_command.""" mock_log.return_value = 'success' obj = RunwayModuleNpm(context=runway_context, path='./tests', options={}) caplog.set_level(logging.INFO, logger='runway') assert not obj.log_npm_command(['npm', 'test']) mock_log.assert_called_once_with(['npm', 'test']) assert ['tests: Running "success"'] == caplog.messages
def test_package_json_missing( self, caplog, patch_module_npm, runway_context, tmp_path ): """Test package_json_missing.""" caplog.set_level(logging.DEBUG, logger="runway") obj = RunwayModuleNpm(context=runway_context, path=str(tmp_path), options={}) assert obj.package_json_missing() assert ["module is missing package.json"] == caplog.messages (tmp_path / "package.json").touch() assert not obj.package_json_missing()
def test_init(self, mock_warn, mock_which, caplog, runway_context, tmp_path): """Test init and the attributes set in init.""" # pylint: disable=no-member caplog.set_level(logging.ERROR, logger='runway') mock_which.side_effect = [True, True, False] options = { 'environments': True, # can only be True of a falsy value (but not dict) 'options': { 'test_option': 'option_value' }, 'parameters': { 'test_parameter': 'parameter_value' }, 'extra': 'something' } obj = RunwayModuleNpm( context=runway_context, # side_effect[0] path=str(tmp_path), options=options.copy()) mock_which.assert_called_once() assert obj.context == runway_context assert obj.environments == options['environments'] assert obj.extra == options['extra'] assert obj.options == options['options'] assert obj.parameters == options['parameters'] assert isinstance(obj.path, Path) assert str(obj.path) == str(tmp_path) mock_warn.assert_called_once_with(runway_context.env_vars) obj = RunwayModuleNpm( context=runway_context, # side_effect[1] path=tmp_path) assert not obj.environments assert not obj.options assert not obj.parameters assert obj.path == tmp_path caplog.clear() with pytest.raises(SystemExit) as excinfo: obj = RunwayModuleNpm( context=runway_context, # side_effect[2] path=tmp_path) assert excinfo.value.code > 0 # non-zero exit code assert caplog.messages == [ '{}: "npm" not found in path or is not executable; ' 'please ensure it is installed correctly.'.format(tmp_path.name) ]
def test_package_json_missing(self, caplog, patch_module_npm, runway_context, tmp_path): """Test package_json_missing.""" caplog.set_level(logging.WARNING, logger='runway') obj = RunwayModuleNpm(context=runway_context, path=str(tmp_path), options={}) assert obj.package_json_missing() assert [ '{}: Module is missing a "package.json"'.format(tmp_path.name) ] == caplog.messages (tmp_path / 'package.json').touch() assert not obj.package_json_missing()
def test_check_for_npm(self, mock_which, caplog, runway_context): """Test check_for_npm.""" mock_which.side_effect = ['something', # first call during init None] # explicit call obj = RunwayModuleNpm(context=runway_context, path='./tests', options={}) caplog.set_level(logging.WARNING, logger='runway') with pytest.raises(SystemExit): assert not obj.check_for_npm() assert ['tests: "npm" not found in path or is not executable; ' 'please ensure it is installed correctly.'] == \ caplog.messages
def test_init(self, mock_warn, mock_which, caplog, runway_context, tmp_path): """Test init and the attributes set in init.""" # pylint: disable=no-member caplog.set_level(logging.ERROR, logger="runway") mock_which.side_effect = [True, True, False] options = { "environments": True, # can only be True of a falsy value (but not dict) "options": {"test_option": "option_value"}, "parameters": {"test_parameter": "parameter_value"}, "extra": "something", } obj = RunwayModuleNpm( context=runway_context, # side_effect[0] path=str(tmp_path), options=options.copy(), ) mock_which.assert_called_once() assert obj.context == runway_context assert obj.environments == options["environments"] assert obj.extra == options["extra"] assert obj.options == options["options"] assert obj.parameters == options["parameters"] assert isinstance(obj.path, Path) assert str(obj.path) == str(tmp_path) mock_warn.assert_called_once_with(runway_context.env_vars) obj = RunwayModuleNpm(context=runway_context, path=tmp_path) # side_effect[1] assert not obj.environments assert not obj.options assert not obj.parameters assert obj.path == tmp_path caplog.clear() with pytest.raises(SystemExit) as excinfo: obj = RunwayModuleNpm( context=runway_context, path=tmp_path # side_effect[2] ) assert excinfo.value.code > 0 # non-zero exit code assert caplog.messages == [ '"npm" not found in path or is not executable; ' "please ensure it is installed correctly" ]
def test_npm_install( self, moc_proc, mock_ci, caplog, monkeypatch, patch_module_npm, runway_context ): """Test npm_install.""" monkeypatch.setattr(runway_context, "no_color", False) caplog.set_level(logging.INFO, logger="runway") obj = RunwayModuleNpm(context=runway_context, path="./tests", options={}) expected_logs = [] expected_calls = [] obj.options["skip_npm_ci"] = True assert not obj.npm_install() expected_logs.append("skipped npm ci/npm install") obj.options["skip_npm_ci"] = False obj.context.env.ci = True mock_ci.return_value = True assert not obj.npm_install() expected_logs.append("running npm ci...") expected_calls.append(call([NPM_BIN, "ci"])) obj.context.env.ci = False assert not obj.npm_install() expected_logs.append("running npm install...") expected_calls.append(call([NPM_BIN, "install"])) obj.context.env.ci = True mock_ci.return_value = False assert not obj.npm_install() expected_logs.append("running npm install...") expected_calls.append(call([NPM_BIN, "install"])) monkeypatch.setattr(runway_context, "no_color", True) assert not obj.npm_install() expected_logs.append("running npm install...") expected_calls.append(call([NPM_BIN, "install", "--no-color"])) obj.options["skip_npm_ci"] = False obj.context.env.ci = True mock_ci.return_value = True assert not obj.npm_install() expected_logs.append("running npm ci...") expected_calls.append(call([NPM_BIN, "ci", "--no-color"])) assert expected_logs == caplog.messages moc_proc.check_call.assert_has_calls(expected_calls)
def test_npm_install(self, moc_proc, mock_ci, caplog, monkeypatch, patch_module_npm, runway_context): """Test npm_install.""" monkeypatch.setattr(runway_context, 'no_color', False) caplog.set_level(logging.INFO, logger='runway') obj = RunwayModuleNpm(context=runway_context, path='./tests', options={}) expected_logs = [] expected_calls = [] obj.options['skip_npm_ci'] = True assert not obj.npm_install() expected_logs.append('tests: Skipping npm ci and npm install...') obj.options['skip_npm_ci'] = False obj.context.env_vars['CI'] = True mock_ci.return_value = True assert not obj.npm_install() expected_logs.append('tests: Running npm ci...') expected_calls.append(call([NPM_BIN, 'ci'])) obj.context.env_vars['CI'] = False assert not obj.npm_install() expected_logs.append('tests: Running npm install...') expected_calls.append(call([NPM_BIN, 'install'])) obj.context.env_vars['CI'] = True mock_ci.return_value = False assert not obj.npm_install() expected_logs.append('tests: Running npm install...') expected_calls.append(call([NPM_BIN, 'install'])) monkeypatch.setattr(runway_context, 'no_color', True) assert not obj.npm_install() expected_logs.append('tests: Running npm install...') expected_calls.append(call([NPM_BIN, 'install', '--no-color'])) obj.options['skip_npm_ci'] = False obj.context.env_vars['CI'] = True mock_ci.return_value = True assert not obj.npm_install() expected_logs.append('tests: Running npm ci...') expected_calls.append(call([NPM_BIN, 'ci', '--no-color'])) assert expected_logs == caplog.messages moc_proc.check_call.assert_has_calls(expected_calls)