我正在使用 go 工具分析代码覆盖率。 首先我创建测试二进制文件
go test -coverpkg="github.com/ypapax/flags" -c
然后我运行它
./flags.test -test.coverprofile=/tmp/out.out
cat /tmp/out.out
并查看个人资料内容
PASS
coverage: 100.0% of statements in github.com/ypapax/flags
mode: set
github.com/ypapax/flags/main.go:5.12,7.2 1 1
问题是:
是否可以预定义标志 -test.coverprofile=/tmp/out.out 这样我就可以在没有 test.coverprofile 标志的情况下运行
./flags.test
猫/tmp/out.out
并且仍然看到 /tmp/out.out 内容。
我的测试代码:
package main
import "testing"
func TestRunMain(t *testing.T) {
main()
}
我的主要代码:
package main
import "time"
func main(){
time.Sleep(time.Second)
}
完整代码在这里:
go get github.com/ypapax/flags && cd $GOPATH/src/github.com/ypapax/flags
我的问题可能看起来很奇怪,但如果已经编写了很多调用 flags.test 二进制的功能测试并且避免添加 -test.coverprofile<> 到每个 flags.test 二进制调用。
我看到了 2 种可能的解决方案:
-test.coverprofile。怎么做?-test.coverprofile 标志的 flags.test。怎么做?我很感激任何建议。谢谢
我正在尝试运行@dmitris 答案
package main
import (
"io/ioutil"
"os/exec"
"log"
"fmt"
. "github.com/jteeuwen/go-bindata"
)
func main() {
prog, err := Asset("flags.test")
if err != nil {
panic(err)
}
dest := "/tmp/flags.test" // could also use https://golang.org/pkg/io/ioutil/#TempFile
coverProfile := "/tmp/flagstest.out"
err = ioutil.WriteFile(dest, prog, 0755)
if err != nil {
panic(err)
}
cmd := exec.Command(dest, "-test.coverprofile", coverProfile)
output, err := cmd.CombinedOutput()
if err != nil {
log.Fatalf("Output: %s, Error: %s", string(output), err)
}
fmt.Println(string(output))
fmt.Println("Coverprofile saved to ", coverProfile)
}
不清楚从哪里导入 Asset func on line prog, err := Asset("flags.test")。
运行 go build -o wrapper wrapper.go 给我
./wrapper.go:13: cannot convert "flags.test" (type string) to type bindata.Asset
./wrapper.go:13: assignment count mismatch: 2 = 1
最佳答案
如果您必须这样做,您可以执行以下操作 - 这个想法是将“原始”测试二进制文件打包到您将分发的二进制文件中,然后解压“原始”文件并使用所需的参数调用它:
go-bindata:go get -u github.com/jteeuwen/go-bindata/...flags.test:go test -c -covergo-bindata -o wrapper.go flags.testmain 函数来提取二进制文件并使用所需参数调用它:包装器.go
func main() {
prog, err := Asset("flags.test")
if err != nil {
panic(err)
}
dest := "/tmp/flags.test" // could also use https://golang.org/pkg/io/ioutil/#TempFile
coverProfile := "/tmp/flagstest.out"
err = ioutil.WriteFile(dest, prog, 0755)
if err != nil {
panic(err)
}
cmd := exec.Command(dest, "-test.coverprofile", coverProfile)
output, err := cmd.CombinedOutput()
if err != nil {
log.Fatalf("Output: %s, Error: %s", string(output), err)
}
fmt.Println(string(output))
fmt.Println("Coverprofile saved to ", coverProfile)
}
go build -o wrapper wrapper.go输出:
$ go run wrapper.go
PASS
coverage: 100.0% of statements
Coverprofile saved to /tmp/flagstest.out
关于bash - 预定义 test.coverprofile 标志,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38787443/
我正在尝试设置一个puppet节点,但rubygems似乎不正常。如果我通过它自己的二进制文件(/usr/lib/ruby/gems/1.8/gems/facter-1.5.8/bin/facter)在cli上运行facter,它工作正常,但如果我通过由rubygems(/usr/bin/facter)安装的二进制文件,它抛出:/usr/lib/ruby/1.8/facter/uptime.rb:11:undefinedmethod`get_uptime'forFacter::Util::Uptime:Module(NoMethodError)from/usr/lib/ruby
我正在尝试测试是否存在表单。我是Rails新手。我的new.html.erb_spec.rb文件的内容是:require'spec_helper'describe"messages/new.html.erb"doit"shouldrendertheform"dorender'/messages/new.html.erb'reponse.shouldhave_form_putting_to(@message)with_submit_buttonendendView本身,new.html.erb,有代码:当我运行rspec时,它失败了:1)messages/new.html.erbshou
我在我的项目中添加了一个系统来重置用户密码并通过电子邮件将密码发送给他,以防他忘记密码。昨天它运行良好(当我实现它时)。当我今天尝试启动服务器时,出现以下错误。=>BootingWEBrick=>Rails3.2.1applicationstartingindevelopmentonhttp://0.0.0.0:3000=>Callwith-dtodetach=>Ctrl-CtoshutdownserverExiting/Users/vinayshenoy/.rvm/gems/ruby-1.9.3-p0/gems/actionmailer-3.2.1/lib/action_mailer
我想向我的Controller传递一个参数,它是一个简单的复选框,但我不知道如何在模型的form_for中引入它,这是我的观点:{:id=>'go_finance'}do|f|%>Transferirde:para:Entrada:"input",:placeholder=>"Quantofoiganho?"%>Saída:"output",:placeholder=>"Quantofoigasto?"%>Nota:我想做一个额外的复选框,但我该怎么做,模型中没有一个对象,而是一个要检查的对象,以便在Controller中创建一个ifelse,如果没有检查,请帮助我,非常感谢,谢谢
我已经从我的命令行中获得了一切,所以我可以运行rubymyfile并且它可以正常工作。但是当我尝试从sublime中运行它时,我得到了undefinedmethod`require_relative'formain:Object有人知道我的sublime设置中缺少什么吗?我正在使用OSX并安装了rvm。 最佳答案 或者,您可以只使用“require”,它应该可以正常工作。我认为“require_relative”仅适用于ruby1.9+ 关于ruby-主要:Objectwhenrun
我正在为一个项目制作一个简单的shell,我希望像在Bash中一样解析参数字符串。foobar"helloworld"fooz应该变成:["foo","bar","helloworld","fooz"]等等。到目前为止,我一直在使用CSV::parse_line,将列分隔符设置为""和.compact输出。问题是我现在必须选择是要支持单引号还是双引号。CSV不支持超过一个分隔符。Python有一个名为shlex的模块:>>>shlex.split("Test'helloworld'foo")['Test','helloworld','foo']>>>shlex.split('Test"
我有一些代码在几个不同的位置之一运行:作为具有调试输出的命令行工具,作为不接受任何输出的更大程序的一部分,以及在Rails环境中。有时我需要根据代码的位置对代码进行细微的更改,我意识到以下样式似乎可行:print"Testingnestedfunctionsdefined\n"CLI=trueifCLIdeftest_printprint"CommandLineVersion\n"endelsedeftest_printprint"ReleaseVersion\n"endendtest_print()这导致:TestingnestedfunctionsdefinedCommandLin
我有一个只接受一个参数的方法:defmy_method(number)end如果使用number调用方法,我该如何引发错误??通常,我如何定义方法参数的条件?比如我想在调用的时候报错:my_method(1) 最佳答案 您可以添加guard在函数的开头,如果参数无效则引发异常。例如:defmy_method(number)failArgumentError,"Inputshouldbegreaterthanorequalto2"ifnumbereputse.messageend#=>Inputshouldbegreaterthano
我使用Ember作为我的前端和GrapeAPI来为我的API提供服务。前端发送类似:{"service"=>{"name"=>"Name","duration"=>"30","user"=>nil,"organization"=>"org","category"=>nil,"description"=>"description","disabled"=>true,"color"=>nil,"availabilities"=>[{"day"=>"Saturday","enabled"=>false,"timeSlots"=>[{"startAt"=>"09:00AM","endAt"=>
我想获取模块中定义的所有常量的值:moduleLettersA='apple'.freezeB='boy'.freezeendconstants给了我常量的名字:Letters.constants(false)#=>[:A,:B]如何获取它们的值的数组,即["apple","boy"]? 最佳答案 为了做到这一点,请使用mapLetters.constants(false).map&Letters.method(:const_get)这将返回["a","b"]第二种方式:Letters.constants(false).map{|c