Skip to content

Commit

Permalink
Merge pull request #56 from boechat107/fix-reading-existing-file
Browse files Browse the repository at this point in the history
Fix reading an existing file
  • Loading branch information
blp1526 authored Aug 16, 2020
2 parents 2778d3f + da6b3ad commit 5600917
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 32 deletions.
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down
53 changes: 23 additions & 30 deletions autoload/storage.vim
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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

Expand All @@ -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

Expand Down
43 changes: 43 additions & 0 deletions spec/storage_spec.vim
Original file line number Diff line number Diff line change
Expand Up @@ -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' . "\<Esc>"
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"'
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 5600917

Please sign in to comment.