From 158ad39bedbeedcfe2d66325ed00750ef6d83259 Mon Sep 17 00:00:00 2001 From: Andre Ambrosio Boechat Date: Tue, 11 Aug 2020 10:09:48 -0300 Subject: [PATCH 1/9] Define "tempfile" from an existent "dict" key and force content download --- autoload/storage.vim | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/autoload/storage.vim b/autoload/storage.vim index 88b76c6..9270721 100644 --- a/autoload/storage.vim +++ b/autoload/storage.vim @@ -4,6 +4,8 @@ function! storage#read(cmd, path, dict) abort if (!has_key(a:dict, a:path)) let tempfile = tempname() . '.' . storage#current_file_extension() let a:dict[a:path] = tempfile + else + let tempfile = a:dict[a:path] endif call storage#get_cmd(a:cmd, a:path, tempfile) silent execute 'edit' fnameescape(tempfile) @@ -101,7 +103,7 @@ function! storage#cmd_script(...) abort endfunction function! storage#get_cmd(cmd, bucket, file) abort - let script = storage#cmd_script(a:cmd, 'get', a:bucket, a:file) + let script = storage#cmd_script(a:cmd, 'get --force', a:bucket, a:file) return storage#run_cmd(script) endfunction From 55a9da2eb9fde19c81b4452805228889f51c5339 Mon Sep 17 00:00:00 2001 From: Andre Ambrosio Boechat Date: Wed, 12 Aug 2020 09:01:51 -0300 Subject: [PATCH 2/9] Don't catch errors in "storage#read" --- autoload/storage.vim | 42 +++++++++++++++++++----------------------- 1 file changed, 19 insertions(+), 23 deletions(-) diff --git a/autoload/storage.vim b/autoload/storage.vim index 9270721..3b67224 100644 --- a/autoload/storage.vim +++ b/autoload/storage.vim @@ -1,27 +1,24 @@ function! storage#read(cmd, path, dict) abort - try - if (storage#last_string(a:path) !=? '/') - if (!has_key(a:dict, a:path)) - let tempfile = tempname() . '.' . storage#current_file_extension() - let a:dict[a:path] = tempfile - else - let tempfile = a:dict[a:path] - endif - call storage#get_cmd(a:cmd, a:path, tempfile) - silent execute 'edit' fnameescape(tempfile) - silent execute '%yank' - setlocal nobuflisted - silent execute 'edit' fnameescape(a:path) - silent execute 'put' - silent execute 'normal ggdd' - silent execute 'filetype detect' + if (storage#last_string(a:path) !=? '/') + if (!has_key(a:dict, a:path)) + let tempfile = tempname() . '.' . storage#current_file_extension() + let a:dict[a:path] = tempfile else - setlocal nomodified - let ls_result = storage#ls_cmd(a:cmd, a:path) - call storage#open_quickfix(ls_result) + let tempfile = a:dict[a:path] endif - catch - endtry + call storage#get_cmd(a:cmd, a:path, tempfile) + silent execute 'edit' fnameescape(tempfile) + silent execute '%yank' + setlocal nobuflisted + silent execute 'edit' fnameescape(a:path) + silent execute 'put' + silent execute 'normal ggdd' + silent execute 'filetype detect' + else + setlocal nomodified + let ls_result = storage#ls_cmd(a:cmd, a:path) + call storage#open_quickfix(ls_result) + endif endfunction function! storage#write(cmd, dict, path) abort @@ -123,8 +120,7 @@ function! storage#run_cmd(script) abort if v:shell_error == 0 return result else - echo result - throw 'Bad Exit Status Error' + throw 'Bad Exit Status Error => ' . trim(result) endif endfunction From 3fa4a4f6140956522c166b365f91235db6a25a10 Mon Sep 17 00:00:00 2001 From: Andre Ambrosio Boechat Date: Wed, 12 Aug 2020 10:12:26 -0300 Subject: [PATCH 3/9] Don't catch errors when executing "storage#put_cmd" --- autoload/storage.vim | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/autoload/storage.vim b/autoload/storage.vim index 3b67224..44c6a12 100644 --- a/autoload/storage.vim +++ b/autoload/storage.vim @@ -34,14 +34,10 @@ function! storage#write(cmd, dict, path) abort silent execute 'normal ggdd' silent execute 'write' setlocal nobuflisted - try - echo storage#put_cmd(a:cmd, tempfile, a:path) - catch - finally - silent execute 'edit' fnameescape(a:path) - endtry - " NOTE: - " Expected to be still 'modified' if storage#put_cmd failed + + echo storage#put_cmd(a:cmd, tempfile, a:path) + + silent execute 'edit' fnameescape(a:path) setlocal nomodified let &hidden = current_hidden endfunction From 99d2c58cff24d88913be3c29bb58d40ff3e3ba55 Mon Sep 17 00:00:00 2001 From: Andre Ambrosio Boechat Date: Fri, 14 Aug 2020 17:46:42 -0300 Subject: [PATCH 4/9] Add a not so simple test for "storage#read" --- spec/storage_spec.vim | 45 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/spec/storage_spec.vim b/spec/storage_spec.vim index ad56d2d..4b3d10b 100644 --- a/spec/storage_spec.vim +++ b/spec/storage_spec.vim @@ -2,6 +2,50 @@ source $PWD/autoload/storage.vim let v:errors = [] +" Redefines a function whose name is 'a:name' using 'a:stub'. +function! Mock_function(name, stub) abort + let l:text =<< trim EOF + function! %s(...) abort closure + return call(a:stub, a:000) + endfunction + EOF + execute printf(join(l:text, "\n"), a:name) +endfunction + +function! Get_stub(cmd, path, tempfile) abort + silent execute 'edit' fnameescape(a:tempfile) + " Delete the file content. + silent execute 'normal! ggdG' + silent execute 'normal! a' . 'bla bla' . "\" + silent execute 'write' + setlocal nobuflisted +endfunction + +call Mock_function('storage#get_cmd', funcref('Get_stub')) + +" This testing function does not work at first time the script is sourced. The +" reason is explained in this link: +" https://stackoverflow.com/a/22633702/747872 +function! Spec_storage_read() abort + echo 'storage#read()' + let current_buffer = @% + let storage_dict = {} + let storage_cmd = 's3cmd' + let file_path = 's3://some-bucket/some-file' + + try + call storage#read(storage_cmd, file_path, storage_dict) + call storage#read(storage_cmd, file_path, storage_dict) + catch + call assert_true(v:exception) + endtry + echo repeat(' ', 2) . 'should not throw an error' + + silent exe 'edit' current_buffer + silent exe 'bd!' file_path + echo "\n" +endfunction + function! Spec_storage_current_file_extension() abort echo 'storage#current_file_extension()' echo repeat(' ', 2).'when current file is "storage_spec.vim"' @@ -48,6 +92,7 @@ function! Spec_storage_errorformatted_string() abort echo "\n" endfunction +call Spec_storage_read() call Spec_storage_current_file_extension() call Spec_storage_cmd_script() call Spec_storage_last_string() From dfc9de97bcab1451391ecc3652680f72bbfa6e03 Mon Sep 17 00:00:00 2001 From: Andre Ambrosio Boechat Date: Fri, 14 Aug 2020 17:53:22 -0300 Subject: [PATCH 5/9] Make things faster by avoiding a file extension for the temporary file name --- autoload/storage.vim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/autoload/storage.vim b/autoload/storage.vim index 44c6a12..b1a3850 100644 --- a/autoload/storage.vim +++ b/autoload/storage.vim @@ -1,7 +1,7 @@ function! storage#read(cmd, path, dict) abort if (storage#last_string(a:path) !=? '/') if (!has_key(a:dict, a:path)) - let tempfile = tempname() . '.' . storage#current_file_extension() + let tempfile = tempname() let a:dict[a:path] = tempfile else let tempfile = a:dict[a:path] From f3c030b5f978f2150b2452b548e4361fb8a879d2 Mon Sep 17 00:00:00 2001 From: Andre Ambrosio Boechat Date: Fri, 14 Aug 2020 17:54:55 -0300 Subject: [PATCH 6/9] Improve info messages for the testing function --- spec/storage_spec.vim | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/spec/storage_spec.vim b/spec/storage_spec.vim index 4b3d10b..9e8e4f9 100644 --- a/spec/storage_spec.vim +++ b/spec/storage_spec.vim @@ -33,13 +33,14 @@ function! Spec_storage_read() abort let storage_cmd = 's3cmd' let file_path = 's3://some-bucket/some-file' + echo repeat(' ', 2) . 'when called twice for the same file' try call storage#read(storage_cmd, file_path, storage_dict) call storage#read(storage_cmd, file_path, storage_dict) catch call assert_true(v:exception) endtry - echo repeat(' ', 2) . 'should not throw an error' + echo repeat(' ', 4) . 'should not throw an error' silent exe 'edit' current_buffer silent exe 'bd!' file_path From 3ad98f55d92d3b85c51ad6730834504995465444 Mon Sep 17 00:00:00 2001 From: Andre Ambrosio Boechat Date: Sat, 15 Aug 2020 19:15:07 -0300 Subject: [PATCH 7/9] Make sure the buffer is not set as modified after a "storage#read" --- autoload/storage.vim | 1 + spec/storage_spec.vim | 5 +---- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/autoload/storage.vim b/autoload/storage.vim index b1a3850..22724a3 100644 --- a/autoload/storage.vim +++ b/autoload/storage.vim @@ -14,6 +14,7 @@ function! storage#read(cmd, path, dict) abort silent execute 'put' silent execute 'normal ggdd' silent execute 'filetype detect' + setlocal nomodified else setlocal nomodified let ls_result = storage#ls_cmd(a:cmd, a:path) diff --git a/spec/storage_spec.vim b/spec/storage_spec.vim index 9e8e4f9..2b52631 100644 --- a/spec/storage_spec.vim +++ b/spec/storage_spec.vim @@ -23,14 +23,11 @@ endfunction call Mock_function('storage#get_cmd', funcref('Get_stub')) -" This testing function does not work at first time the script is sourced. The -" reason is explained in this link: -" https://stackoverflow.com/a/22633702/747872 function! Spec_storage_read() abort echo 'storage#read()' let current_buffer = @% let storage_dict = {} - let storage_cmd = 's3cmd' + let storage_cmd = 'whatever' let file_path = 's3://some-bucket/some-file' echo repeat(' ', 2) . 'when called twice for the same file' From d470ef46ad4864e0b343a627926063e1c344971a Mon Sep 17 00:00:00 2001 From: Andre Ambrosio Boechat Date: Sat, 15 Aug 2020 19:21:04 -0300 Subject: [PATCH 8/9] Update README with info about the "get" command and how to run the tests --- README.md | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index fdc91de..7ab9af4 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,9 @@ This executes `s3cmd ls s3://BUCKET/OBJECT/`, and shows the result as a quickfix ## Requirement -The [s3cmd](https://github.com/s3tools/s3cmd) cli tool, or a same `get`, `put` and `ls` interfafce cli tool. +The [s3cmd](https://github.com/s3tools/s3cmd) cli tool, or a same `get --force` +(overwrites the local file if it already exists), `put` and `ls` interfafce cli +tool. ## Option @@ -53,7 +55,10 @@ set runtimepath^=~/.vim/bundle/storage.vim ## Development -This repository's [spec](https://github.com/blp1526/storage.vim/tree/master/spec) directory has test code. If you want to run test code, open spec file, and exec `source %`. +This repository's +[spec](https://github.com/blp1526/storage.vim/tree/master/spec) directory has +test code. If you want to run test code, open spec file with +`vim --clean spec/storage_spec.vim`, and exec `source %`. ## Contributing From da6b3ad467176b9f93e5635a56ee79f6e805ac19 Mon Sep 17 00:00:00 2001 From: Andre Ambrosio Boechat Date: Sat, 15 Aug 2020 19:22:53 -0300 Subject: [PATCH 9/9] Remove some extra blank lines --- autoload/storage.vim | 2 -- 1 file changed, 2 deletions(-) diff --git a/autoload/storage.vim b/autoload/storage.vim index 22724a3..38022c2 100644 --- a/autoload/storage.vim +++ b/autoload/storage.vim @@ -35,9 +35,7 @@ function! storage#write(cmd, dict, path) abort silent execute 'normal ggdd' silent execute 'write' setlocal nobuflisted - echo storage#put_cmd(a:cmd, tempfile, a:path) - silent execute 'edit' fnameescape(a:path) setlocal nomodified let &hidden = current_hidden