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 diff --git a/autoload/storage.vim b/autoload/storage.vim index 88b76c6..38022c2 100644 --- a/autoload/storage.vim +++ b/autoload/storage.vim @@ -1,25 +1,25 @@ 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 - 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() + 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' + setlocal nomodified + 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 @@ -35,14 +35,8 @@ 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 @@ -101,7 +95,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 @@ -121,8 +115,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 diff --git a/spec/storage_spec.vim b/spec/storage_spec.vim index ad56d2d..2b52631 100644 --- a/spec/storage_spec.vim +++ b/spec/storage_spec.vim @@ -2,6 +2,48 @@ 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')) + +function! Spec_storage_read() abort + echo 'storage#read()' + let current_buffer = @% + let storage_dict = {} + let storage_cmd = 'whatever' + 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(' ', 4) . '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 +90,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()