Browse Source

test AutoFile#Size (happy path)

pull/2799/head
Anton Kaliaev 6 years ago
parent
commit
1944d8534b
1 changed files with 37 additions and 0 deletions
  1. +37
    -0
      libs/autofile/autofile_test.go

+ 37
- 0
libs/autofile/autofile_test.go View File

@ -84,3 +84,40 @@ func TestOpenAutoFilePerms(t *testing.T) {
t.Errorf("unexpected error %v", e)
}
}
func TestAutoFileSize(t *testing.T) {
// First, create an AutoFile writing to a tempfile dir
f, err := ioutil.TempFile("", "sighup_test")
require.NoError(t, err)
err = f.Close()
require.NoError(t, err)
// Here is the actual AutoFile.
af, err := OpenAutoFile(f.Name())
require.NoError(t, err)
// 1. Empty file
size, err := af.Size()
require.Zero(t, size)
require.NoError(t, err)
// 2. Not empty file
data := []byte("Maniac\n")
_, err = af.Write(data)
require.NoError(t, err)
size, err = af.Size()
require.EqualValues(t, len(data), size)
require.NoError(t, err)
// 3. Not existing file
err = af.Close()
require.NoError(t, err)
err = os.Remove(f.Name())
require.NoError(t, err)
size, err = af.Size()
require.EqualValues(t, 0, size, "Expected a new file to be empty")
require.NoError(t, err)
// Cleanup
_ = os.Remove(f.Name())
}

Loading…
Cancel
Save