def clifactory(tmpdir): appdir = tmpdir.mkdir('app') appdir.join('app.py').write( '# Test app\n' 'import chalice\n' 'app = chalice.Chalice(app_name="test")\n' ) chalice_dir = appdir.mkdir('.chalice') chalice_dir.join('config.json').write('{}') return factory.CLIFactory(str(appdir))
def _run_cli_command(runner, function, args, cli_factory=None): # Handles passing in 'obj' so we can get commands # that use @pass_context to work properly. # click doesn't support this natively so we have to duplicate # what 'def cli(...)' is doing. if cli_factory is None: cli_factory = factory.CLIFactory('.') result = runner.invoke( function, args, obj={'project_dir': '.', 'debug': False, 'factory': cli_factory}) return result
def test_can_load_project_config_after_project_creation(runner): with runner.isolated_filesystem(): result = runner.invoke(cli.new_project, ['testproject']) assert result.exit_code == 0 config = factory.CLIFactory('testproject').load_project_config() assert config == { 'version': '2.0', 'app_name': 'testproject', 'stages': { 'dev': {'api_gateway_stage': u'dev'} } }
def test_does_deploy_with_default_api_gateway_stage_name( runner, mock_cli_factory): with runner.isolated_filesystem(): cli.create_new_project_skeleton('testproject') os.chdir('testproject') # This isn't perfect as we're assuming we know how to # create the config_obj like the deploy() command does, # it should give us more confidence that the api gateway # stage defaults are still working. cli_factory = factory.CLIFactory('.') config = cli_factory.create_config_obj(chalice_stage_name='dev', autogen_policy=None, api_gateway_stage=None) assert config.api_gateway_stage == DEFAULT_APIGATEWAY_STAGE_NAME
def test_packaging_requirements_keeps_same_hash(self, runner, app_skeleton): req = os.path.join(app_skeleton, 'requirements.txt') package = 'botocore==1.12.202' with open(req, 'w') as f: f.write('%s\n' % package) cli_factory = factory.CLIFactory(app_skeleton) package_output_location = os.path.join(app_skeleton, 'pkg') self._run_package_cmd(package_output_location, app_skeleton, cli_factory, runner) original_checksum = self._calculate_checksum(package_output_location) self._run_package_cmd(package_output_location, app_skeleton, cli_factory, runner) new_checksum = self._calculate_checksum(package_output_location) assert original_checksum == new_checksum
def test_preserves_executable_permissions(self, runner, app_skeleton): vendor = os.path.join(app_skeleton, 'vendor') os.makedirs(vendor) executable_file = os.path.join(vendor, 'myscript.sh') with open(executable_file, 'w') as f: f.write('#!/bin/bash\necho foo\n') os.chmod(executable_file, 0o755) cli_factory = factory.CLIFactory(app_skeleton) package_output_location = os.path.join(app_skeleton, 'pkg') self._run_package_cmd(package_output_location, app_skeleton, cli_factory, runner) self._verify_file_is_executable(package_output_location, 'myscript.sh') original_checksum = self._calculate_checksum(package_output_location) self._run_package_cmd(package_output_location, app_skeleton, cli_factory, runner) new_checksum = self._calculate_checksum(package_output_location) assert original_checksum == new_checksum
def test_does_not_package_bad_requirements_file( self, runner, app_skeleton): req = os.path.join(app_skeleton, 'requirements.txt') package = _get_random_package_name() with open(req, 'w') as f: f.write('%s\n' % package) cli_factory = factory.CLIFactory(app_skeleton) # Try to build a deployment package from the bad requirements file. # It should fail with a NoSuchPackageError error since the package # should not exist. result = runner.invoke( cli.package, ['package'], obj={'project_dir': app_skeleton, 'debug': False, 'factory': cli_factory}) assert result.exception is not None ex = result.exception assert isinstance(ex, NoSuchPackageError) assert str(ex) == 'Could not satisfy the requirement: %s' % package
def assert_can_package_dependency( self, runner, app_skeleton, package, contents): req = os.path.join(app_skeleton, 'requirements.txt') with open(req, 'w') as f: f.write('%s\n' % package) cli_factory = factory.CLIFactory(app_skeleton) package_output_location = os.path.join(app_skeleton, 'pkg') result = runner.invoke( cli.package, [package_output_location], obj={'project_dir': app_skeleton, 'debug': False, 'factory': cli_factory}) assert result.exit_code == 0 assert result.output.strip() == 'Creating deployment package.' package_path = os.path.join(app_skeleton, 'pkg', 'deployment.zip') package_file = ZipFile(package_path) package_content = package_file.namelist() for content in contents: assert content in package_content
def test_can_package_with_dashes_in_name(self, runner, app_skeleton): req = os.path.join(app_skeleton, 'requirements.txt') package = 'googleapis-common-protos==1.5.2' with open(req, 'w') as f: f.write('%s\n' % package) cli_factory = factory.CLIFactory(app_skeleton) package_output_location = os.path.join(app_skeleton, 'pkg') result = runner.invoke(cli.package, [package_output_location], obj={ 'project_dir': app_skeleton, 'debug': False, 'factory': cli_factory }) assert result.exit_code == 0 assert result.output.strip() == 'Creating deployment package.' package_path = os.path.join(app_skeleton, 'pkg', 'deployment.zip') package_file = ZipFile(package_path) package_content = package_file.namelist() assert 'google/api/__init__.py' in package_content
def test_default_new_project_adds_index_route(runner): with runner.isolated_filesystem(): result = runner.invoke(cli.new_project, ['testproject']) assert result.exit_code == 0 app = factory.CLIFactory('testproject').load_chalice_app() assert '/' in app.routes