2017-02-04 66 views
1

似乎無法找到此答案。Travis-CI GoLang示例測試錯誤

我正在嘗試使用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", "[email protected]") 

    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>[email protected] (Jane Doe)</managingEditor> 
    //  <pubDate>Wed, 01 Feb 2017 07:51:00 -0500</pubDate> 
    //  <image> 
    //  <url>http://example.com/podcast.jpg</url> 
    //  </image> 
    //  <itunes:author>[email protected] (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>[email protected] (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>[email protected] (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> 
} 

下面就來具體的特拉維斯構建失敗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實例中本地運行。

這是隻有特拉維斯不能運行go test,舉例。

如果我跳過實例,然後通過測試用:

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

但是,這違背了目的:

  1. 驗證的例子
  2. 從97%降價的代碼覆蓋率報告降至43%

(是的,我使用範例作爲代碼覆蓋的一部分 - 一部分的博客p ost我試圖寫,並使用特拉維斯來顯示它。但是,呃,哎呀!特拉維斯錯誤!)

我懷疑特拉維斯是幹什麼用stderr和/或stdout時髦的東西,將輸出重定向到控制檯/日誌這可能會阻止go test從看到的結果。

回答

3

特拉維斯得到的和想要的值不相同。它們與時區偏移量不同。

time.Local在您的本地計算機上爲-0500,在Travis上爲+0000。使用time.UTC或與代碼運行的系統無關的其他某個位置運行:

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

omg,我不能這樣做!這就是我在半夜與孩子們一起編碼的過程。謝謝! – eduncan911