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
    }
    
    ...
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度