doutan3463
2018-10-24 19:36 阅读 133
已采纳

使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 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
    }
    
    ...
    
    点赞 评论 复制链接分享

相关推荐