Ai
1 Star 0 Fork 120

elddriver/mind-cluster

forked from Ascend/mind-cluster
暂停
 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
file_watcher_test.go 2.47 KB
一键复制 编辑 原始数据 按行查看 历史
/* Copyright(C) 2025. Huawei Technologies Co.,Ltd. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// Package utils test for file watcher utils
package utils
import (
"errors"
"fmt"
"os"
"testing"
"github.com/agiledragon/gomonkey/v2"
"github.com/fsnotify/fsnotify"
"github.com/smartystreets/goconvey/convey"
)
var testErr = errors.New("test error")
const (
testFilePath = "./test.txt"
errFilePath = "./not_exist_file.txt"
)
func TestGetFileWatcherChan(t *testing.T) {
prepareTestFile(t)
defer removeFile()
p1 := gomonkey.ApplyFuncReturn(PathStringChecker, "", nil)
defer p1.Reset()
convey.Convey("test func GetFileWatcherChan success", t, func() {
_, err := GetFileWatcherChan(testFilePath)
convey.So(err, convey.ShouldBeNil)
})
convey.Convey("test func GetFileWatcherChan failed, new watcher err", t, func() {
p2 := gomonkey.ApplyFuncReturn(fsnotify.NewWatcher, nil, testErr)
defer p2.Reset()
_, err := GetFileWatcherChan(testFilePath)
expErr := fmt.Errorf("new file watcher failed, error: %v", testErr)
convey.So(err, convey.ShouldResemble, expErr)
})
convey.Convey("test func GetFileWatcherChan failed, file does not exist", t, func() {
_, err := GetFileWatcherChan(errFilePath)
expErr := fmt.Sprintf("watch file <%s> failed", errFilePath)
convey.So(err.Error(), convey.ShouldContainSubstring, expErr)
})
convey.Convey("test func GetFileWatcherChan failed, watcher is nil", t, func() {
var watcher = &FileWatcher{}
eventCh := watcher.Events()
convey.So(eventCh, convey.ShouldBeNil)
errCh := watcher.Errors()
convey.So(errCh, convey.ShouldBeNil)
err := watcher.Close()
convey.So(err, convey.ShouldBeNil)
})
}
func prepareTestFile(t *testing.T) {
const mode644 = 0644
err := os.WriteFile(testFilePath, []byte("file context"), mode644)
if err != nil {
t.Error(err)
}
}
func removeFile() {
if err := os.Remove(testFilePath); err != nil && errors.Is(err, os.ErrNotExist) {
fmt.Printf("remove file %s failed, %v\n", testFilePath, err)
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/elddriver/mind-cluster.git
git@gitee.com:elddriver/mind-cluster.git
elddriver
mind-cluster
mind-cluster
366d0f7fc38e

搜索帮助