generated from OtusGolang/home_work
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
DimVlas
committed
Jun 18, 2024
1 parent
88f0a3c
commit 1f618fe
Showing
6 changed files
with
360 additions
and
5 deletions.
There are no files selected for viewing
Empty file.
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 |
---|---|---|
@@ -1,7 +1,187 @@ | ||
package main | ||
|
||
import "testing" | ||
import ( | ||
"fmt" | ||
"log" | ||
"os" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestCopy(t *testing.T) { | ||
// Place your code here. | ||
newFile, err := os.CreateTemp("./testdata/", "test_file_*.txt") | ||
if err != nil { | ||
panic(err) | ||
} | ||
defer func() { | ||
os.Remove(newFile.Name()) | ||
}() | ||
|
||
size, err := newFile.WriteString("Необходимо реализовать утилиту копирования файлов\n") // size = 95 | ||
if err != nil { | ||
t.Error(err) | ||
return | ||
} | ||
|
||
log.Println(size) | ||
|
||
t.Run("no error full file", func(t *testing.T) { | ||
toName := "./testdata/out_test_file.txt" | ||
err := Copy(newFile.Name(), toName, 0, 0) | ||
|
||
require.NoError(t, err) | ||
require.FileExistsf(t, toName, "file must %v exists", toName) | ||
|
||
toFile, _ := os.Open(toName) | ||
toFileInfo, _ := toFile.Stat() | ||
require.Equalf(t, int64(size), toFileInfo.Size(), "file size must be %v", size) | ||
|
||
defer os.Remove(toFile.Name()) | ||
}) | ||
|
||
t.Run("no error size less limit", func(t *testing.T) { | ||
toName := "./testdata/out_test_file.txt" | ||
err := Copy(newFile.Name(), toName, 90, 30) | ||
|
||
require.NoError(t, err) | ||
require.FileExistsf(t, toName, "file must %v exists", toName) | ||
|
||
toFile, _ := os.Open(toName) | ||
toFileInfo, _ := toFile.Stat() | ||
require.Equalf(t, int64(5), toFileInfo.Size(), "file size must be %v", 5) | ||
|
||
defer os.Remove(toFile.Name()) | ||
}) | ||
} | ||
|
||
func TestFileCreate(t *testing.T) { | ||
t.Run("not regular file", func(t *testing.T) { | ||
fromPath := "/dev/urandom" | ||
|
||
toFile, tstErr := fileCreate(fromPath) | ||
defer func() { | ||
if toFile == nil { | ||
return | ||
} | ||
errCl := toFile.Close() | ||
if errCl != nil { | ||
panic(errCl) | ||
} | ||
}() | ||
|
||
require.EqualError(t, tstErr, ErrUnsupportedFile.Error(), "actual err - %v", tstErr) | ||
require.Nil(t, toFile, "file must be nil") | ||
}) | ||
|
||
t.Run("no error new file", func(t *testing.T) { | ||
toPath := fmt.Sprintf("./testdata/out_test_create_%v.txt", time.DateTime) | ||
|
||
toFile, tstErr := fileCreate(toPath) | ||
defer func() { | ||
if toFile == nil { | ||
return | ||
} | ||
|
||
if errCl := toFile.Close(); errCl != nil { | ||
panic(errCl) | ||
} | ||
if errRm := os.Remove(toFile.Name()); errRm != nil { | ||
panic(errRm) | ||
} | ||
}() | ||
|
||
require.NoError(t, tstErr) | ||
require.NotNil(t, toFile, "file must not be nil") | ||
}) | ||
|
||
t.Run("no error exist file", func(t *testing.T) { | ||
newFile, err := os.CreateTemp("./testdata/", "out_test_create_*.txt") | ||
if err != nil { | ||
panic(err) | ||
} | ||
defer func() { | ||
os.Remove(newFile.Name()) | ||
}() | ||
|
||
toPath := newFile.Name() | ||
toFile, tstErr := fileCreate(toPath) | ||
defer func() { | ||
if toFile == nil { | ||
return | ||
} | ||
errCl := toFile.Close() | ||
if errCl != nil { | ||
panic(errCl) | ||
} | ||
}() | ||
|
||
require.NoError(t, tstErr) | ||
require.NotNil(t, toFile, "file must not be nil") | ||
}) | ||
} | ||
|
||
func TestFileOpen(t *testing.T) { | ||
t.Run("not regular file", func(t *testing.T) { | ||
var offset int64 = 1000 | ||
|
||
fromPath := "/dev/urandom" | ||
|
||
fromFile, fromSize, tstErr := fileOpen(fromPath, offset) | ||
defer func() { | ||
if fromFile == nil { | ||
return | ||
} | ||
errCl := fromFile.Close() | ||
if errCl != nil { | ||
panic(errCl) | ||
} | ||
}() | ||
|
||
require.EqualError(t, tstErr, ErrUnsupportedFile.Error(), "actual err - %v", tstErr) | ||
require.Nil(t, fromFile, "file must be nil") | ||
require.Equal(t, fromSize, int64(0), "file size must be 0") | ||
}) | ||
|
||
t.Run("big file", func(t *testing.T) { | ||
var offset int64 = 1000 | ||
fromPath := "./testdata/out_offset6000_limit1000.txt" | ||
|
||
fromFile, fromSize, tstErr := fileOpen(fromPath, offset) | ||
defer func() { | ||
if fromFile == nil { | ||
return | ||
} | ||
errCl := fromFile.Close() | ||
if errCl != nil { | ||
panic(errCl) | ||
} | ||
}() | ||
|
||
require.EqualError(t, tstErr, ErrOffsetExceedsFileSize.Error(), "actual err - %v", tstErr) | ||
require.Nil(t, fromFile, "file must be nil") | ||
require.Equal(t, fromSize, int64(0), "file size must be 0") | ||
}) | ||
|
||
t.Run("no error", func(t *testing.T) { | ||
var offset int64 = 100 | ||
|
||
fromPath := "./testdata/out_offset6000_limit1000.txt" | ||
|
||
fromFile, fromSize, tstErr := fileOpen(fromPath, offset) | ||
log.Println(fromSize) | ||
defer func() { | ||
if fromFile == nil { | ||
return | ||
} | ||
errCl := fromFile.Close() | ||
if errCl != nil { | ||
panic(errCl) | ||
} | ||
}() | ||
require.NoError(t, tstErr) | ||
require.NotNil(t, fromFile, "file must not be nil") | ||
require.Equalf(t, fromSize, int64(617), "file size must be %v", fromSize) | ||
}) | ||
} |
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,11 @@ | ||
module github.com/fixme_my_friend/hw07_file_copying | ||
module github.com/DimVlas/hw07_file_copying | ||
|
||
go 1.19 | ||
|
||
require github.com/stretchr/testify v1.9.0 | ||
|
||
require ( | ||
github.com/davecgh/go-spew v1.1.1 // indirect | ||
github.com/pmezard/go-difflib v1.0.0 // indirect | ||
gopkg.in/yaml.v3 v3.0.1 // indirect | ||
) |
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,10 @@ | ||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= | ||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= | ||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= | ||
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= | ||
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= | ||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= | ||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= | ||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= | ||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= |
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