-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from hypnoglow/feature-add-file
Feature add file
- Loading branch information
Showing
7 changed files
with
299 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
language: go | ||
go: | ||
- tip | ||
- tip | ||
before_install: | ||
- go get github.com/mattn/goveralls | ||
script: | ||
- $HOME/gopath/bin/goveralls -service=travis-ci |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"os" | ||
"path" | ||
) | ||
|
||
// Copy copies a file from src to dst. | ||
func Copy(src, dst string) error { | ||
sfi, err := os.Lstat(src) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if !sfi.Mode().IsRegular() { | ||
return fmt.Errorf("Non-regular source file %s (%q)", sfi.Name(), sfi.Mode().String()) | ||
} | ||
|
||
dfi, err := os.Stat(dst) | ||
if err != nil { | ||
if !os.IsNotExist(err) { | ||
return err | ||
} | ||
// file not exists - do not do anything | ||
} else { | ||
// file exists - check it | ||
if !(dfi.Mode().IsRegular()) { | ||
return fmt.Errorf("Non-regular destination file %s (%q)", dfi.Name(), dfi.Mode().String()) | ||
} | ||
} | ||
|
||
err = copyFileContents(src, dst) | ||
return err | ||
} | ||
|
||
// copyFileContents copies the contents of the file named src to the file named | ||
// by dst. The file will be created if it does not already exist. If the | ||
// destination file exists, all it's contents will be replaced by the contents | ||
// of the source file. | ||
func copyFileContents(src, dst string) (err error) { | ||
in, err := os.Open(src) | ||
if err != nil { | ||
return | ||
} | ||
|
||
defer in.Close() | ||
|
||
err = os.MkdirAll(path.Dir(dst), 0755) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
out, err := os.Create(dst) | ||
if err != nil { | ||
return | ||
} | ||
|
||
defer func() { | ||
cerr := out.Close() | ||
if err == nil { | ||
err = cerr | ||
} | ||
}() | ||
|
||
if _, err = io.Copy(out, in); err != nil { | ||
return err | ||
} | ||
|
||
err = out.Sync() | ||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
package main | ||
|
||
import ( | ||
"io/ioutil" | ||
"os" | ||
"path" | ||
"testing" | ||
) | ||
|
||
func TestCopy(t *testing.T) { | ||
testCopyPositive(t) | ||
testCopyNegativeLstat(t) | ||
testCopyNegativeSymlink(t) | ||
} | ||
|
||
func testCopyPositive(t *testing.T) { | ||
// set up | ||
|
||
src := "/tmp/dotbro/fileutils/original.txt" | ||
content := []byte("Some Content") | ||
|
||
if err := os.MkdirAll(path.Dir(src), 0755); err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if err := ioutil.WriteFile(src, content, 0755); err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
// test | ||
|
||
dest := "/tmp/dotbro/fileutils/copy.txt" | ||
if err := Copy(src, dest); err != nil { | ||
t.Error(err) | ||
} | ||
|
||
copyContent, err := ioutil.ReadFile(dest) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
|
||
if string(copyContent) != string(content) { | ||
t.Error(err) | ||
} | ||
|
||
// tear down | ||
|
||
if err := os.Remove(src); err != nil { | ||
t.Error(err) | ||
} | ||
|
||
if err := os.Remove(dest); err != nil { | ||
t.Error(err) | ||
} | ||
} | ||
|
||
func testCopyNegativeLstat(t *testing.T) { | ||
// set up | ||
|
||
src := "/tmp/dotbro/fileutils/original.txt" | ||
|
||
if err := os.MkdirAll(path.Dir(src), 0755); err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
// no read permissions | ||
if err := ioutil.WriteFile(src, nil, 0333); err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
// test | ||
|
||
dest := "/tmp/dotbro/fileutils/copy.txt" | ||
err := Copy(dest, dest) | ||
if err == nil { | ||
t.Error("No error!") | ||
} | ||
|
||
// tear down | ||
|
||
if err := os.Remove(src); err != nil { | ||
t.Error(err) | ||
} | ||
} | ||
|
||
func testCopyNegativeSymlink(t *testing.T) { | ||
// set up | ||
|
||
original := "/tmp/dotbro/fileutils/original.txt" | ||
|
||
if err := os.MkdirAll(path.Dir(original), 0755); err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if err := ioutil.WriteFile(original, nil, 0755); err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
symlink := "/tmp/dotbro/fileutils/symlink" | ||
if err := os.Symlink(original, symlink); err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
// test | ||
|
||
dest := "/tmp/dotbro/fileutils/symlink-copy.txt" | ||
err := Copy(symlink, dest) | ||
if err == nil { | ||
t.Error("No error!") | ||
} | ||
|
||
// tear down | ||
|
||
if err := os.Remove(original); err != nil { | ||
t.Error(err) | ||
} | ||
|
||
if err := os.Remove(symlink); err != nil { | ||
t.Error(err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters