【问题标题】:Travis-CI GoLang Examples test errorTravis-CI GoLang 示例测试错误
【发布时间】:2017-02-04 01:01:07
【问题描述】:

似乎找不到这个问题的答案。

我正在尝试使用 Travis-CI 为我的 GoLang 包构建/运行测试;但是,Travis 在作为测试的一部分运行的 GoLang 示例上一直失败。

例如,这里有一个例子:

func Example() {
    now := time.Date(2017, time.February, 1, 7, 51, 0, 0, time.Local)

    // instantiate a new Podcast
    p := podcast.New(
        "Sample Podcasts",
        "http://example.com/",
        "An example Podcast",
        &now, &now,
    )

    // add some channel properties
    p.ISubtitle = "A simple Podcast"
    p.AddImage("http://example.com/podcast.jpg")
    p.AddAuthor("Jane Doe", "jane.doe@example.com")

    for i := int64(0); i < 2; i++ {
        n := strconv.FormatInt(i, 10)

        // create an Item
        item := podcast.Item{
            Title:       "Episode " + n,
            Description: "Description for Episode " + n,
            ISubtitle:   "A simple episode " + n,
            PubDate:     &now,
        }
        // add a Download to the Item
        item.AddEnclosure("http://example.com/"+n+".mp3", podcast.MP3, 55*(i+1))

        // add the Item and check for validation errors
        if _, err := p.AddItem(item); err != nil {
            os.Stderr.WriteString("item validation error: " + err.Error())
        }
    }

    // Podcast.Encode writes to an io.Writer
    if err := p.Encode(os.Stdout); err != nil {
        os.Stderr.WriteString("error writing to stdout: " + err.Error())
    }

    // Output:
    // <?xml version="1.0" encoding="UTF-8"?>
    // <rss version="2.0" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd">
    //   <channel>
    //     <title>Sample Podcasts</title>
    //     <link>http://example.com/</link>
    //     <description>An example Podcast</description>
    //     <generator>go podcast v1.0.0 (github.com/eduncan911/podcast)</generator>
    //     <language>en-us</language>
    //     <lastBuildDate>Wed, 01 Feb 2017 07:51:00 -0500</lastBuildDate>
    //     <managingEditor>jane.doe@example.com (Jane Doe)</managingEditor>
    //     <pubDate>Wed, 01 Feb 2017 07:51:00 -0500</pubDate>
    //     <image>
    //       <url>http://example.com/podcast.jpg</url>
    //     </image>
    //     <itunes:author>jane.doe@example.com (Jane Doe)</itunes:author>
    //     <itunes:subtitle>A simple Podcast</itunes:subtitle>
    //     <itunes:image href="http://example.com/podcast.jpg"></itunes:image>
    //     <item>
    //       <guid>http://example.com/0.mp3</guid>
    //       <title>Episode 0</title>
    //       <link>http://example.com/0.mp3</link>
    //       <description>Description for Episode 0</description>
    //       <pubDate>Wed, 01 Feb 2017 07:51:00 -0500</pubDate>
    //       <enclosure url="http://example.com/0.mp3" length="55" type="audio/mpeg"></enclosure>
    //       <itunes:author>jane.doe@example.com (Jane Doe)</itunes:author>
    //       <itunes:subtitle>A simple episode 0</itunes:subtitle>
    //       <itunes:image href="http://example.com/podcast.jpg"></itunes:image>
    //       <itunes:duration>55</itunes:duration>
    //     </item>
    //     <item>
    //       <guid>http://example.com/1.mp3</guid>
    //       <title>Episode 1</title>
    //       <link>http://example.com/1.mp3</link>
    //       <description>Description for Episode 1</description>
    //       <pubDate>Wed, 01 Feb 2017 07:51:00 -0500</pubDate>
    //       <enclosure url="http://example.com/1.mp3" length="110" type="audio/mpeg"></enclosure>
    //       <itunes:author>jane.doe@example.com (Jane Doe)</itunes:author>
    //       <itunes:subtitle>A simple episode 1</itunes:subtitle>
    //       <itunes:image href="http://example.com/podcast.jpg"></itunes:image>
    //       <itunes:duration>110</itunes:duration>
    //     </item>
    //   </channel>
    // </rss>
}

这是一个指向失败的特定 Travis 构建的链接 go test,即使它在本地运行:

https://travis-ci.org/eduncan911/podcast/builds/198093154

$ go test -covermode count -coverprofile cover.out
--- FAIL: Example (0.00s)
got:
<?xml version="1.0" encoding="UTF-8"?>
...(omitted)

want:
<?xml version="1.0" encoding="UTF-8"?>
...(omitted)

go test 命令在 macOS 本地、Windows 10 VM 和 EC2 Ubuntu 实例中正常工作。

只有 Travis 无法运行go test,并附上示例。

如果我跳过示例,则测试通过:

$ go test -run Test -covermode count -coverprofile cover.out

但这违背了以下目的:

  1. 验证示例
  2. 将代码覆盖率报告从 97% 降至 43%

(是的,我使用示例作为代码覆盖的一部分 - 我正在尝试编写的博客文章的一部分,并使用 Travis 来展示它。但是,呃,哎呀!travis 错误!)

我怀疑 Travis 正在使用 stderr 和/或 stdout 做一些时髦的事情,将输出重定向到控制台/日志,这可能会阻止 go test 看到结果。

【问题讨论】:

    标签: go travis-ci


    【解决方案1】:

    Travis 上的 got 和 want 值不相同。它们的不同之处在于时区偏移量。

    time.Local 在本地机器上是 -0500,在 Travis 上是 +0000。使用time.UTC 或独立于运行代码的系统的其他位置:

    now := time.Date(2017, time.February, 1, 7, 51, 0, 0, time.UTC)
    

    【讨论】:

    • 天哪,我不能这样!这就是我在半夜和孩子们一起编码所得到的。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-11-28
    • 1970-01-01
    • 1970-01-01
    • 2018-02-03
    • 1970-01-01
    • 2018-04-30
    • 1970-01-01
    相关资源
    最近更新 更多