Travis-CI GoLang示例测试错误

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

Travis-CI GoLang Examples test error

问题

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

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

例如,这是一个示例:

  1. func Example() {
  2. now := time.Date(2017, time.February, 1, 7, 51, 0, 0, time.Local)
  3. // instantiate a new Podcast
  4. p := podcast.New(
  5. "Sample Podcasts",
  6. "http://example.com/",
  7. "An example Podcast",
  8. &now, &now,
  9. )
  10. // add some channel properties
  11. p.ISubtitle = "A simple Podcast"
  12. p.AddImage("http://example.com/podcast.jpg")
  13. p.AddAuthor("Jane Doe", "jane.doe@example.com")
  14. for i := int64(0); i < 2; i++ {
  15. n := strconv.FormatInt(i, 10)
  16. // create an Item
  17. item := podcast.Item{
  18. Title: "Episode " + n,
  19. Description: "Description for Episode " + n,
  20. ISubtitle: "A simple episode " + n,
  21. PubDate: &now,
  22. }
  23. // add a Download to the Item
  24. item.AddEnclosure("http://example.com/"+n+".mp3", podcast.MP3, 55*(i+1))
  25. // add the Item and check for validation errors
  26. if _, err := p.AddItem(item); err != nil {
  27. os.Stderr.WriteString("item validation error: " + err.Error())
  28. }
  29. }
  30. // Podcast.Encode writes to an io.Writer
  31. if err := p.Encode(os.Stdout); err != nil {
  32. os.Stderr.WriteString("error writing to stdout: " + err.Error())
  33. }
  34. // Output:
  35. // <?xml version="1.0" encoding="UTF-8"?>
  36. // <rss version="2.0" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd">
  37. // <channel>
  38. // <title>Sample Podcasts</title>
  39. // <link>http://example.com/</link>
  40. // <description>An example Podcast</description>
  41. // <generator>go podcast v1.0.0 (github.com/eduncan911/podcast)</generator>
  42. // <language>en-us</language>
  43. // <lastBuildDate>Wed, 01 Feb 2017 07:51:00 -0500</lastBuildDate>
  44. // <managingEditor>jane.doe@example.com (Jane Doe)</managingEditor>
  45. // <pubDate>Wed, 01 Feb 2017 07:51:00 -0500</pubDate>
  46. // <image>
  47. // <url>http://example.com/podcast.jpg</url>
  48. // </image>
  49. // <itunes:author>jane.doe@example.com (Jane Doe)</itunes:author>
  50. // <itunes:subtitle>A simple Podcast</itunes:subtitle>
  51. // <itunes:image href="http://example.com/podcast.jpg"></itunes:image>
  52. // <item>
  53. // <guid>http://example.com/0.mp3</guid>
  54. // <title>Episode 0</title>
  55. // <link>http://example.com/0.mp3</link>
  56. // <description>Description for Episode 0</description>
  57. // <pubDate>Wed, 01 Feb 2017 07:51:00 -0500</pubDate>
  58. // <enclosure url="http://example.com/0.mp3" length="55" type="audio/mpeg"></enclosure>
  59. // <itunes:author>jane.doe@example.com (Jane Doe)</itunes:author>
  60. // <itunes:subtitle>A simple episode 0</itunes:subtitle>
  61. // <itunes:image href="http://example.com/podcast.jpg"></itunes:image>
  62. // <itunes:duration>55</itunes:duration>
  63. // </item>
  64. // <item>
  65. // <guid>http://example.com/1.mp3</guid>
  66. // <title>Episode 1</title>
  67. // <link>http://example.com/1.mp3</link>
  68. // <description>Description for Episode 1</description>
  69. // <pubDate>Wed, 01 Feb 2017 07:51:00 -0500</pubDate>
  70. // <enclosure url="http://example.com/1.mp3" length="110" type="audio/mpeg"></enclosure>
  71. // <itunes:author>jane.doe@example.com (Jane Doe)</itunes:author>
  72. // <itunes:subtitle>A simple episode 1</itunes:subtitle>
  73. // <itunes:image href="http://example.com/podcast.jpg"></itunes:image>
  74. // <itunes:duration>110</itunes:duration>
  75. // </item>
  76. // </channel>
  77. // </rss>
  78. }

这是一个失败的Travis构建的链接,go test失败,尽管在本地运行正常:

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

  1. $ go test -covermode count -coverprofile cover.out
  2. --- FAIL: Example (0.00s)
  3. got:
  4. <?xml version="1.0" encoding="UTF-8"?>
  5. ...(省略)
  6. want:
  7. <?xml version="1.0" encoding="UTF-8"?>
  8. ...(省略)

go test命令在本地的macOS、Windows 10虚拟机和EC2 Ubuntu实例上正常工作。

只有Travis无法运行带有示例的go test

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

  1. $ 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:

  1. func Example() {
  2. now := time.Date(2017, time.February, 1, 7, 51, 0, 0, time.Local)
  3. // instantiate a new Podcast
  4. p := podcast.New(
  5. &quot;Sample Podcasts&quot;,
  6. &quot;http://example.com/&quot;,
  7. &quot;An example Podcast&quot;,
  8. &amp;now, &amp;now,
  9. )
  10. // add some channel properties
  11. p.ISubtitle = &quot;A simple Podcast&quot;
  12. p.AddImage(&quot;http://example.com/podcast.jpg&quot;)
  13. p.AddAuthor(&quot;Jane Doe&quot;, &quot;jane.doe@example.com&quot;)
  14. for i := int64(0); i &lt; 2; i++ {
  15. n := strconv.FormatInt(i, 10)
  16. // create an Item
  17. item := podcast.Item{
  18. Title: &quot;Episode &quot; + n,
  19. Description: &quot;Description for Episode &quot; + n,
  20. ISubtitle: &quot;A simple episode &quot; + n,
  21. PubDate: &amp;now,
  22. }
  23. // add a Download to the Item
  24. item.AddEnclosure(&quot;http://example.com/&quot;+n+&quot;.mp3&quot;, podcast.MP3, 55*(i+1))
  25. // add the Item and check for validation errors
  26. if _, err := p.AddItem(item); err != nil {
  27. os.Stderr.WriteString(&quot;item validation error: &quot; + err.Error())
  28. }
  29. }
  30. // Podcast.Encode writes to an io.Writer
  31. if err := p.Encode(os.Stdout); err != nil {
  32. os.Stderr.WriteString(&quot;error writing to stdout: &quot; + err.Error())
  33. }
  34. // Output:
  35. // &lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
  36. // &lt;rss version=&quot;2.0&quot; xmlns:itunes=&quot;http://www.itunes.com/dtds/podcast-1.0.dtd&quot;&gt;
  37. // &lt;channel&gt;
  38. // &lt;title&gt;Sample Podcasts&lt;/title&gt;
  39. // &lt;link&gt;http://example.com/&lt;/link&gt;
  40. // &lt;description&gt;An example Podcast&lt;/description&gt;
  41. // &lt;generator&gt;go podcast v1.0.0 (github.com/eduncan911/podcast)&lt;/generator&gt;
  42. // &lt;language&gt;en-us&lt;/language&gt;
  43. // &lt;lastBuildDate&gt;Wed, 01 Feb 2017 07:51:00 -0500&lt;/lastBuildDate&gt;
  44. // &lt;managingEditor&gt;jane.doe@example.com (Jane Doe)&lt;/managingEditor&gt;
  45. // &lt;pubDate&gt;Wed, 01 Feb 2017 07:51:00 -0500&lt;/pubDate&gt;
  46. // &lt;image&gt;
  47. // &lt;url&gt;http://example.com/podcast.jpg&lt;/url&gt;
  48. // &lt;/image&gt;
  49. // &lt;itunes:author&gt;jane.doe@example.com (Jane Doe)&lt;/itunes:author&gt;
  50. // &lt;itunes:subtitle&gt;A simple Podcast&lt;/itunes:subtitle&gt;
  51. // &lt;itunes:image href=&quot;http://example.com/podcast.jpg&quot;&gt;&lt;/itunes:image&gt;
  52. // &lt;item&gt;
  53. // &lt;guid&gt;http://example.com/0.mp3&lt;/guid&gt;
  54. // &lt;title&gt;Episode 0&lt;/title&gt;
  55. // &lt;link&gt;http://example.com/0.mp3&lt;/link&gt;
  56. // &lt;description&gt;Description for Episode 0&lt;/description&gt;
  57. // &lt;pubDate&gt;Wed, 01 Feb 2017 07:51:00 -0500&lt;/pubDate&gt;
  58. // &lt;enclosure url=&quot;http://example.com/0.mp3&quot; length=&quot;55&quot; type=&quot;audio/mpeg&quot;&gt;&lt;/enclosure&gt;
  59. // &lt;itunes:author&gt;jane.doe@example.com (Jane Doe)&lt;/itunes:author&gt;
  60. // &lt;itunes:subtitle&gt;A simple episode 0&lt;/itunes:subtitle&gt;
  61. // &lt;itunes:image href=&quot;http://example.com/podcast.jpg&quot;&gt;&lt;/itunes:image&gt;
  62. // &lt;itunes:duration&gt;55&lt;/itunes:duration&gt;
  63. // &lt;/item&gt;
  64. // &lt;item&gt;
  65. // &lt;guid&gt;http://example.com/1.mp3&lt;/guid&gt;
  66. // &lt;title&gt;Episode 1&lt;/title&gt;
  67. // &lt;link&gt;http://example.com/1.mp3&lt;/link&gt;
  68. // &lt;description&gt;Description for Episode 1&lt;/description&gt;
  69. // &lt;pubDate&gt;Wed, 01 Feb 2017 07:51:00 -0500&lt;/pubDate&gt;
  70. // &lt;enclosure url=&quot;http://example.com/1.mp3&quot; length=&quot;110&quot; type=&quot;audio/mpeg&quot;&gt;&lt;/enclosure&gt;
  71. // &lt;itunes:author&gt;jane.doe@example.com (Jane Doe)&lt;/itunes:author&gt;
  72. // &lt;itunes:subtitle&gt;A simple episode 1&lt;/itunes:subtitle&gt;
  73. // &lt;itunes:image href=&quot;http://example.com/podcast.jpg&quot;&gt;&lt;/itunes:image&gt;
  74. // &lt;itunes:duration&gt;110&lt;/itunes:duration&gt;
  75. // &lt;/item&gt;
  76. // &lt;/channel&gt;
  77. // &lt;/rss&gt;
  78. }

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

  1. $ go test -covermode count -coverprofile cover.out
  2. --- FAIL: Example (0.00s)
  3. got:
  4. &lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
  5. ...(omitted)
  6. want:
  7. &lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
  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:

  1. $ 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或其他与代码运行的系统无关的位置:

  1. 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:

  1. 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:

确定