From a7b54a5f7e1d83d67980108c3bfa4edde1de64bc Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Fri, 1 Apr 2022 21:23:04 +0800 Subject: [PATCH] add test for CopyFile, fix lint --- models/unittest/fscopy.go | 4 ++++ modules/cache/cache_redis.go | 1 - modules/util/legacy_test.go | 27 +++++++++++++++++++++++++++ modules/util/string.go | 4 ++++ modules/util/string_test.go | 4 ++++ 5 files changed, 39 insertions(+), 1 deletion(-) diff --git a/models/unittest/fscopy.go b/models/unittest/fscopy.go index 40ca7a6b105c..ff815e729d47 100644 --- a/models/unittest/fscopy.go +++ b/models/unittest/fscopy.go @@ -1,3 +1,7 @@ +// Copyright 2022 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + package unittest import ( diff --git a/modules/cache/cache_redis.go b/modules/cache/cache_redis.go index a80a47570f8c..a19667064ba3 100644 --- a/modules/cache/cache_redis.go +++ b/modules/cache/cache_redis.go @@ -39,7 +39,6 @@ func asString(v interface{}) string { default: panic("current redis cache doesn't support non-string data type") } - return "" } // Put puts value (string type) into cache with key and expire time. diff --git a/modules/util/legacy_test.go b/modules/util/legacy_test.go index a9c43e357327..c41f7a008c22 100644 --- a/modules/util/legacy_test.go +++ b/modules/util/legacy_test.go @@ -7,11 +7,38 @@ package util import ( "crypto/aes" "crypto/rand" + "fmt" + "os" "testing" + "time" "github.com/stretchr/testify/assert" ) +func TestCopyFile(t *testing.T) { + testContent := []byte("hello") + + tmpDir := os.TempDir() + now := time.Now() + srcFile := fmt.Sprintf("%s/copy-test-%d-src.txt", tmpDir, now.UnixMicro()) + dstFile := fmt.Sprintf("%s/copy-test-%d-dst.txt", tmpDir, now.UnixMicro()) + + _ = os.Remove(srcFile) + _ = os.Remove(dstFile) + defer func() { + _ = os.Remove(srcFile) + _ = os.Remove(dstFile) + }() + + err := os.WriteFile(srcFile, testContent, 0o777) + assert.NoError(t, err) + err = CopyFile(srcFile, dstFile) + assert.NoError(t, err) + dstContent, err := os.ReadFile(dstFile) + assert.NoError(t, err) + assert.Equal(t, testContent, dstContent) +} + func TestAESGCM(t *testing.T) { t.Parallel() diff --git a/modules/util/string.go b/modules/util/string.go index 27185d19feef..4301f75f99c9 100644 --- a/modules/util/string.go +++ b/modules/util/string.go @@ -1,3 +1,7 @@ +// Copyright 2022 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + package util import "github.com/yuin/goldmark/util" diff --git a/modules/util/string_test.go b/modules/util/string_test.go index ae45ced88fa1..49de29ab67a9 100644 --- a/modules/util/string_test.go +++ b/modules/util/string_test.go @@ -1,3 +1,7 @@ +// Copyright 2022 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + package util import (