Skip to content

Instantly share code, notes, and snippets.

@dillonhafer
Last active May 23, 2023 21:46
Show Gist options
  • Save dillonhafer/f107a37ab1c6a5bf528309a68ea61ce8 to your computer and use it in GitHub Desktop.
Save dillonhafer/f107a37ab1c6a5bf528309a68ea61ce8 to your computer and use it in GitHub Desktop.
go tests
#!/usr/bin/env ruby
file = ARGV[0]
line_num = ARGV[1].to_i
TestFn = Struct.new(:name, :start_line, :end_line)
tests = []
current_test = nil
File.read(file).lines.each.with_index do |line, i|
if line.start_with?("func Test")
name = line.match(/func (Test[^\(]*)/).captures.first
current_test = TestFn.new(name, i+1)
end
if line.start_with?("}")
current_test.end_line = i+1
tests << current_test
current_test = nil
end
end
test_on_line = tests.find do |t|
t.start_line <= line_num && t.end_line >= line_num
end
tests_to_run = test_on_line&.name || tests.map(&:name).join("|")
puts "\e[#{32}m---->\e[0m go test -run #{tests_to_run}"
exec "cd #{File.dirname(file)} && go test -run '#{tests_to_run}' && cd - > /dev/null"
@dillonhafer
Copy link
Author

Run entire file with gotest.rb /path/to/main_test.go or run the single test from line 48 with gotest.rb /path/to/main_test.go 48

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment