Skip to content

Commit

Permalink
Move logic to CommandParams and update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kyleknap committed Oct 22, 2015
1 parent e81ac19 commit 5c31ead
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 23 deletions.
28 changes: 14 additions & 14 deletions awscli/customizations/s3/subcommands.py
Original file line number Diff line number Diff line change
Expand Up @@ -750,20 +750,6 @@ def run(self):
}
result_queue = queue.Queue()

# If the user provided local path does not exist, hard fail because
# we know that we will not be able to upload the file.
if 'locals3' == paths_type and not self.parameters['is_stream']:
if not os.path.exists(files['src']['path']):
raise RuntimeError(
'The user-provided path %s does not exist.' %
files['src']['path'])
# If the operation is downloading to a directory that does not exist,
# create the directories so no warnings are thrown during the syncing
# process.
elif 's3local' == paths_type and files['dir_op']:
if not os.path.exists(files['dest']['path']):
os.makedirs(files['dest']['path'])

operation_name = cmd_translation[paths_type][self.cmd]
file_generator = FileGenerator(self._source_client,
operation_name,
Expand Down Expand Up @@ -926,6 +912,20 @@ def _validate_path_args(self):
raise ValueError("Cannot mv a file onto itself: '%s' - '%s'" % (
params['src'], params['dest']))

# If the user provided local path does not exist, hard fail because
# we know that we will not be able to upload the file.
if 'locals3' == params['paths_type'] and not params['is_stream']:
if not os.path.exists(params['src']):
raise RuntimeError(
'The user-provided path %s does not exist.' %
params['src'])
# If the operation is downloading to a directory that does not exist,
# create the directories so no warnings are thrown during the syncing
# process.
elif 's3local' == params['paths_type'] and params['dir_op']:
if not os.path.exists(params['dest']):
os.makedirs(params['dest'])

def _same_path(self, src, dest):
if not self.parameters['paths_type'] == 's3s3':
return False
Expand Down
32 changes: 23 additions & 9 deletions tests/unit/customizations/s3/test_subcommands.py
Original file line number Diff line number Diff line change
Expand Up @@ -620,25 +620,25 @@ def test_check_src_path_pass(self):
cmd_parameter.check_src_path(filename[0])

def test_validate_streaming_paths_upload(self):
parameters = {'src': '-', 'dest': 's3://bucket'}
cmd_params = CommandParameters('cp', parameters, '')
cmd_params._validate_streaming_paths()
paths = ['-', 's3://bucket']
cmd_params = CommandParameters('cp', {}, '')
cmd_params.add_paths(paths)
self.assertTrue(cmd_params.parameters['is_stream'])
self.assertTrue(cmd_params.parameters['only_show_errors'])
self.assertFalse(cmd_params.parameters['dir_op'])

def test_validate_streaming_paths_download(self):
parameters = {'src': 'localfile', 'dest': '-'}
cmd_params = CommandParameters('cp', parameters, '')
cmd_params._validate_streaming_paths()
paths = ['s3://bucket/key', '-']
cmd_params = CommandParameters('cp', {}, '')
cmd_params.add_paths(paths)
self.assertTrue(cmd_params.parameters['is_stream'])
self.assertTrue(cmd_params.parameters['only_show_errors'])
self.assertFalse(cmd_params.parameters['dir_op'])

def test_validate_no_streaming_paths(self):
parameters = {'src': 'localfile', 'dest': 's3://bucket'}
cmd_params = CommandParameters('cp', parameters, '')
cmd_params._validate_streaming_paths()
paths = [self.file_creator.rootdir, 's3://bucket']
cmd_params = CommandParameters('cp', {}, '')
cmd_params.add_paths(paths)
self.assertFalse(cmd_params.parameters['is_stream'])

def test_validate_streaming_paths_error(self):
Expand All @@ -647,6 +647,20 @@ def test_validate_streaming_paths_error(self):
with self.assertRaises(ValueError):
cmd_params._validate_streaming_paths()

def test_validate_non_existent_local_path_upload(self):
non_existent_path = os.path.join(self.file_creator.rootdir, 'foo')
paths = [non_existent_path, 's3://bucket/']
cmd_param = CommandParameters('cp', {}, '')
with self.assertRaises(RuntimeError):
cmd_param.add_paths(paths)

def test_add_path_for_non_existsent_local_path_download(self):
non_existent_path = os.path.join(self.file_creator.rootdir, 'foo')
paths = ['s3://bucket', non_existent_path]
cmd_param = CommandParameters('cp', {'dir_op': True}, '')
cmd_param.add_paths(paths)
self.assertTrue(os.path.exists(non_existent_path))


class HelpDocTest(BaseAWSHelpOutputTest):
def setUp(self):
Expand Down

0 comments on commit 5c31ead

Please sign in to comment.