douwei4370 2017-02-04 01:01
浏览 60
已采纳

Travis-CI GoLang示例测试错误

Can't seem to find an answer to this one.

I am trying to use Travis-CI to build/run tests for my GoLang package; but, Travis keeps failing on GoLang Examples that are running as part of the tests.

For example, here's one Example:

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>
}

Here's a link to a specific Travis build that fails go test, even though it runs locally:

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 command works normally locally on macOS, in a Windows 10 VM and in an EC2 Ubuntu instances.

It is only Travis that is not able to to run go test, with examples.

If I skip the Examples, then the tests pass with:

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

But that defeats the purpose of:

  1. Validating the Examples
  2. Drops code-coverage report from 97% down to 43%

(yes, I use Examples as part of code coverage - part of a blog post I am trying to write, and to use Travis to show it. but, uh, oops! travis errors!)

I suspect Travis is doing something funky with stderr and/or stdout, redirecting the output to the console/log which may prevent go test from seeing the results.

  • 写回答

1条回答 默认 最新

  • doutan5724 2017-02-04 02:04
    关注

    The got and want values on Travis are not identical. They differ by the time zone offset.

    time.Local is -0500 on your local machine and +0000 on Travis. Use time.UTC or some other location that's independent of the system where the code is running running:

    now := time.Date(2017, time.February, 1, 7, 51, 0, 0, time.UTC)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?