From 5c31eadee8f61cdffd8d5aace20de65f556da73c Mon Sep 17 00:00:00 2001 From: kyleknap Date: Thu, 22 Oct 2015 10:46:37 -0700 Subject: [PATCH] Move logic to CommandParams and update tests --- awscli/customizations/s3/subcommands.py | 28 ++++++++-------- .../customizations/s3/test_subcommands.py | 32 +++++++++++++------ 2 files changed, 37 insertions(+), 23 deletions(-) diff --git a/awscli/customizations/s3/subcommands.py b/awscli/customizations/s3/subcommands.py index ddbcd7323db2..773b0a98c9fb 100644 --- a/awscli/customizations/s3/subcommands.py +++ b/awscli/customizations/s3/subcommands.py @@ -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, @@ -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 diff --git a/tests/unit/customizations/s3/test_subcommands.py b/tests/unit/customizations/s3/test_subcommands.py index e46eb086bf14..db3a6126e176 100644 --- a/tests/unit/customizations/s3/test_subcommands.py +++ b/tests/unit/customizations/s3/test_subcommands.py @@ -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): @@ -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):