Travis-CI GoLang示例测试错误

huangapple go评论89阅读模式
英文:

Travis-CI GoLang Examples test error

问题

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

我正在尝试使用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"?>
...(省略)

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

go test命令在本地的macOS、Windows 10虚拟机和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看到结果。

英文:

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(
&quot;Sample Podcasts&quot;,
&quot;http://example.com/&quot;,
&quot;An example Podcast&quot;,
&amp;now, &amp;now,
)
// add some channel properties
p.ISubtitle = &quot;A simple Podcast&quot;
p.AddImage(&quot;http://example.com/podcast.jpg&quot;)
p.AddAuthor(&quot;Jane Doe&quot;, &quot;jane.doe@example.com&quot;)
for i := int64(0); i &lt; 2; i++ {
n := strconv.FormatInt(i, 10)
// create an Item
item := podcast.Item{
Title:       &quot;Episode &quot; + n,
Description: &quot;Description for Episode &quot; + n,
ISubtitle:   &quot;A simple episode &quot; + n,
PubDate:     &amp;now,
}
// add a Download to the Item
item.AddEnclosure(&quot;http://example.com/&quot;+n+&quot;.mp3&quot;, podcast.MP3, 55*(i+1))
// add the Item and check for validation errors
if _, err := p.AddItem(item); err != nil {
os.Stderr.WriteString(&quot;item validation error: &quot; + err.Error())
}
}
// Podcast.Encode writes to an io.Writer
if err := p.Encode(os.Stdout); err != nil {
os.Stderr.WriteString(&quot;error writing to stdout: &quot; + err.Error())
}
// Output:
// &lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
// &lt;rss version=&quot;2.0&quot; xmlns:itunes=&quot;http://www.itunes.com/dtds/podcast-1.0.dtd&quot;&gt;
//   &lt;channel&gt;
//     &lt;title&gt;Sample Podcasts&lt;/title&gt;
//     &lt;link&gt;http://example.com/&lt;/link&gt;
//     &lt;description&gt;An example Podcast&lt;/description&gt;
//     &lt;generator&gt;go podcast v1.0.0 (github.com/eduncan911/podcast)&lt;/generator&gt;
//     &lt;language&gt;en-us&lt;/language&gt;
//     &lt;lastBuildDate&gt;Wed, 01 Feb 2017 07:51:00 -0500&lt;/lastBuildDate&gt;
//     &lt;managingEditor&gt;jane.doe@example.com (Jane Doe)&lt;/managingEditor&gt;
//     &lt;pubDate&gt;Wed, 01 Feb 2017 07:51:00 -0500&lt;/pubDate&gt;
//     &lt;image&gt;
//       &lt;url&gt;http://example.com/podcast.jpg&lt;/url&gt;
//     &lt;/image&gt;
//     &lt;itunes:author&gt;jane.doe@example.com (Jane Doe)&lt;/itunes:author&gt;
//     &lt;itunes:subtitle&gt;A simple Podcast&lt;/itunes:subtitle&gt;
//     &lt;itunes:image href=&quot;http://example.com/podcast.jpg&quot;&gt;&lt;/itunes:image&gt;
//     &lt;item&gt;
//       &lt;guid&gt;http://example.com/0.mp3&lt;/guid&gt;
//       &lt;title&gt;Episode 0&lt;/title&gt;
//       &lt;link&gt;http://example.com/0.mp3&lt;/link&gt;
//       &lt;description&gt;Description for Episode 0&lt;/description&gt;
//       &lt;pubDate&gt;Wed, 01 Feb 2017 07:51:00 -0500&lt;/pubDate&gt;
//       &lt;enclosure url=&quot;http://example.com/0.mp3&quot; length=&quot;55&quot; type=&quot;audio/mpeg&quot;&gt;&lt;/enclosure&gt;
//       &lt;itunes:author&gt;jane.doe@example.com (Jane Doe)&lt;/itunes:author&gt;
//       &lt;itunes:subtitle&gt;A simple episode 0&lt;/itunes:subtitle&gt;
//       &lt;itunes:image href=&quot;http://example.com/podcast.jpg&quot;&gt;&lt;/itunes:image&gt;
//       &lt;itunes:duration&gt;55&lt;/itunes:duration&gt;
//     &lt;/item&gt;
//     &lt;item&gt;
//       &lt;guid&gt;http://example.com/1.mp3&lt;/guid&gt;
//       &lt;title&gt;Episode 1&lt;/title&gt;
//       &lt;link&gt;http://example.com/1.mp3&lt;/link&gt;
//       &lt;description&gt;Description for Episode 1&lt;/description&gt;
//       &lt;pubDate&gt;Wed, 01 Feb 2017 07:51:00 -0500&lt;/pubDate&gt;
//       &lt;enclosure url=&quot;http://example.com/1.mp3&quot; length=&quot;110&quot; type=&quot;audio/mpeg&quot;&gt;&lt;/enclosure&gt;
//       &lt;itunes:author&gt;jane.doe@example.com (Jane Doe)&lt;/itunes:author&gt;
//       &lt;itunes:subtitle&gt;A simple episode 1&lt;/itunes:subtitle&gt;
//       &lt;itunes:image href=&quot;http://example.com/podcast.jpg&quot;&gt;&lt;/itunes:image&gt;
//       &lt;itunes:duration&gt;110&lt;/itunes:duration&gt;
//     &lt;/item&gt;
//   &lt;/channel&gt;
// &lt;/rss&gt;
}

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:
&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
...(omitted)
want:
&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
...(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

得分: 3

在Travis上,got和want的值并不相同。它们之间的差异在于时区偏移。

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

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

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)

huangapple
  • 本文由 发表于 2017年2月4日 09:01:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/42035363.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定