doutan3463 2018-10-24 19:36
浏览 442
已采纳

使os.Stat引发错误,以使IsNotExist返回false

I'm trying to test the following branch:

if _, err := os.Stat(path); err != nil {
        if os.IsNotExist(err) {
            continue
        }
        return errors.File().AddDetails(err)
    }

Obviously, os.Stat is going to throw an error if path doesn't exist. Reading the Golang documentation returns no details about the errors that os.Stat could return. Is there way to have os.Stat throw another kind of error?

  • 写回答

1条回答 默认 最新

  • doumi7854 2018-10-26 21:12
    关注

    I think you will find it very difficult to control what errors are thrown and when by os.Stat in a platform independent way from a unit test. If you really need a test for the path where an unknown error type is returned, your best bet might be to refactor your package code so that you can mock os.Stat. Although you can't change the behavior of os.Stat directly, by taking advantage of the fact that Go has first-class functions, you can use a bit of indirection to mock it with very minimal changes to your code. If your package code looks like this:

    package mypackage
    
    import "os"
    
    ...
    
        if _, err := os.Stat(path); err != nil {
            if os.IsNotExist(err) {
               continue
            }
            return errors.File().AddDetails(err)
        }
    
    ...
    

    Try refactoring it to use a non-exported package-scope function variable which is assigned os.Stat:

    package mypackage
    
    import "os"
    
    var osStat = os.Stat
    
    ...
    
        if _, err := osStat(path); err != nil {
            if os.IsNotExist(err) {
               continue
            }
            return errors.File().AddDetails(err)
        }
    
    ...
    

    Now, in your test code (which should be in the same package as the code under test), you can reassign osStat to any function with the same signature in order to mock it:

    package mypackage
    
    import (
            "os"
            "testing"
    )
    
    func TestNotExistError(t *testing.T) {
        osStat = func(string) (os.FileInfo, error) {
            return nil, os.ErrNotExist
        }
        // in case other test functions depend on the unmocked behavior
        defer func() {
            osStat = os.Stat
        }()
    
        // rest of the test which triggers the codepath above
    }
    
    func TestOtherError(t *testing.T) {
        osStat = func(string) (os.FileInfo, error) {
            return nil, os.ErrInvalid
        }
        // in case other test functions depend on the unmocked behavior
        defer func() {
            osStat = os.Stat
        }()
    
        // rest of the test which triggers the codepath above
    }
    
    ...
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮