1. 程式人生 > >Go: Testing Standard Library Changes

Go: Testing Standard Library Changes

Go: Testing Standard Library Changes

I had a wonderful time at GopherCon 2017. One of the highlights was being a mentor at the Go Contributor Workshop. This involved two 1.5-hour sessions where Go developers of all skill levels were walked through the process of making their first contributions to the Go project. Every table at the session had two or more mentors to help. Mentors included members of the Go team and community members familiar with the process.

Any contribution to open source can be intimidating, but a large project like Go even more so. Then there’s Gerrit (the code review tool used by Go) and the Contributor License Agreement (CLA) to contend with. While there are reasons for these complications, I don’t think anyone would claim the process is as easy as it should be. The workshop was a great step to getting more people involved in the project. Big shout out to

Steve Francia, Jessie Frazelle, and everyone else involved in making it happen!

The Problem

Now to the meat of this article. During the workshop contributors were encouraged to write a new example for standard library functions. Go examples can be written to be testable, and if you’re going to write a new test, it’s probably a good idea to run that test before submitting to Gerrit.

This can present a problem. Normally, running tests involves running go test [package], which executes the tests with the version of Go currently installed. In most cases this is what you would want, and it will work for some standard library packages, but sometimes it will present confusing error messages.

$ cd ~/golang/src/go/types
$ go test
# _/Users/kale/golang/src/go/types
api_test.go:14:2: use of internal package not allowed
FAIL _/Users/kale/golang/src/go/types [setup failed]

In the example above, one of the test files imports "internal/testenv”, because go resolves to my default 1.8 install the toolchain attempts to import that package from [Go 1.8 install]/src/internal/testenv. This is not allowed because internal packages can only be imported if they share a common root directory.

A Solution?

I could try setting GOROOT to match the development checkout of Go.

$ GOROOT="$HOME/golang" go test -v
go: open /Users/kale/golang/src/runtime/internal/sys/zversion.go: no such file or directory

Now there’s a different error, this one is because zversion.go is generated when the toolchain is built and embeds the Go version in the binary.

We could probably hack our way around this too, and we might be able to get this working in packages with simpler dependencies, but there are a number of other mismatches that may come up. So let’s do it the right way.

The Right Way

What we really want to do is run the tests with a version of the Go toolchain built from the source we’re working on.

# Path that the Go repo was cloned to.
$ GODEV=$HOME/golang
# Move into the src directory.
$ cd $GODEV/src
# Set the bootstrap Go install to the current installation.
$ GOROOT_BOOTSTRAP=$(go env GOROOT) ./make.bash
[output omitted]
# Use the newly built toolchain to run the test.
$ $GODEV/bin/go test -v go/types
[output truncated]
=== RUN ExampleInfo
--- PASS: ExampleInfo (0.00s)
PASS
ok go/types 7.129s

In this case we didn’t have to set GOROOT at all. When the toolchain is built the build location will become the default GOROOT.

Note: If you haveGOROOT set already, you will need to change it or unset it for this to work.

Building the toolchain can take a couple minutes. make.bash builds, but does not run all the tests. all.bash builds and runs tests, which will take much longer.

Note: For Windows there are make.bat and all.bat.

The toolchain doesn’t need to be rebuilt every time a change is made unless you’re modifying and testing the toolchain itself.

Future

Based on my observations at the workshop, I opened https://golang.org/issue/21037 to at least make the build process a little easier by removing the need to explicitly set GOROOT_BOOTSTRAP.

PS: Thanks to Steven Klassen for proof reading this post.

Additional Reading

Workshop Slides:

相關推薦

Go: Testing Standard Library Changes

Go: Testing Standard Library ChangesI had a wonderful time at GopherCon 2017. One of the highlights was being a mentor at the Go Contributor Workshop. This

Python Standard Library

pos 介紹 win .net let keyword xpath imp 9.4 出自:http://blog.csdn.net/qiqll/article/details/39339319 0.1. 關於本書 0.2. 代碼約定 0.3. 關於例

筆記-python-standard library-8.1 data types-datetime

none struct supported 方法 all min lag ons ros 筆記-python-standard library-8.1 data types-datetime 1. Datatimes 本章節內容為日期和時間處理類和方法。

筆記-python-standard library-9.6 random

conf next step += spi red exp option server 筆記-python-standard library-9.6 random 1. random source code:Lib/random.py 1.1. func

筆記-python-standard library-11.2 os.path

exist time split case linu basename 當前 輸出 字符轉換 筆記-python-standard library-11.2 os.path 1. os.path Source code: Lib/posixpath.py (f

筆記-python-standard library-17.7 queue

 筆記-python-standard library-17.7 queue   1.  queue source code:Lib/queue.py 該模組實現了多生產者,多消費者佇列。 此模組實現了所有的required locking semantics. 模組

筆記-python-standard library-16.3 time

輸出 bsp sleep 定義 最適 元組 enter local int 筆記-python-standard library-16.3 time 1. time 1.1. 開始 time模塊中時間表現的格式主要有三種: timestamp時間戳,

go testing:單元測試

Test & Benchmark 原始碼檔名需要以”_test.go”結尾 放置於需要測試模組的相同目錄下 在build時會忽略所有test檔案 Tests 定義 func TestXxx(*testing.T) Benchm

Advanced Go Testing Tutorial

Welcome fellow coders! In this tutorial, we are going to be taking a look at selection of more advanced testing practices used by the likes of the

The Python 2 Standard Library by Example

Distributed with every copy of Python, the Standard Library contains hundreds of modules that provide tools for interacting with the operating system, i

Python3.5.2 document學習系列之02、The Python Standard Library(python 標準庫)——介紹

1.介紹Python庫包含幾種不同種類的組成部分。它包含通常被認為是一種語言“核心”部分的資料型別,如數字和列表。對這些型別來說,Python語言核心定義了書寫上的格式和位置以及一些語義上的約束,但沒有完全定義語法(另一方面,語言核心定義了語法特性像拼寫和操作優先順序)這個庫

Swift Standard Library

Swift has 74 built-in functions but only seven of them are documented in the Swift book (“The Swift Programming Language”). The rest r

go testing

如何保證程式碼的質量和可靠性?Golang自帶了testing包可用來實現測試用例和效能測試. 如下為例,新建gotest專案目錄,編寫兩個檔案bubblesort.go和bubblesort_test.go bubblesort.go package gotest//

Android新提供的測試框架支援庫學習 && Testing Support Library

本文半翻譯與google官方文件,在學習之後,需要了解的是android提供的測試庫工具,以及瞭解它們的作用,以前都是用junit3寫robotium的,現在與時俱進吧。 android提供多重測試用的支援庫,比如說junit4的支援以及ui測試。這一

Python手冊(Standard Library)--datetime+time+calendar

datetime datetime模組定義了6個類 datetime.date 表示日期的類 datetime.datetime 表示日期時間的類 datetime.time 表示時間的類 datetime.timedelta

標準標簽庫JSTL(JSP Standard Tag Library)

when 標準 表達 XML 只有一個 默認 with 本地 輸出變量 1, 核心標簽(最常用, 最重要的) 表達式控制標簽 out 輸出常量 value---直接賦值 輸出變量 default---默認值 escapeXml--

STL(Standard Template Library)

float hms stl containe 排序 Go iterator 操作 eight 容器(Containers) list、deque、vector、map等 算法(Algorithms) 算法作用於容器,它們提供了執行各種操作的方式。包括了對容器的

<Standard Template Library>標準模板庫專項複習總結

看了看部落格園的申請時間也一年多了...想想自己一年多以來一直處於各種划水狀態,現在又要面臨ACM的衝擊... 還是要抓緊時間趕緊複習一下了- -畢竟校園新生賽還是有獎金的..   1.棧   先進後出(LIFO)表 標頭檔案:#include<stack> 變數

<Standard Template Library>標準模板庫專項複習總結(二)

4.佇列 先進先出(FIFO)表 標頭檔案:#include<queue> 變數的定義:queue<TYPE>queueName 成員函式: bool empty() 空佇列返回true,否則返回false void pop

Cpp Chapter 16: The string Class and the Standard Template Library

(這已經是第二次部落格園吞我東西了,有點心態爆炸) 16.1 The string Class ) Constructing a string Here is a table which shows the seven form of constructors that the string class