onsi / ginkgo

A Modern Testing Framework for Go
http://onsi.github.io/ginkgo/
MIT License
8.32k stars 657 forks source link

`gofmt` examples in README #1283

Closed jtarchie closed 6 months ago

jtarchie commented 1 year ago

As an open-source maintainer, I know how helpful having valid working examples in the documentation can be. This is an attempt to update the examples to be copy-pasted.

This transformation was part programmatic, part manual editing. I wrote a little Ruby script that took the code example between the triple-backticks, then tried various "fixes".

If it could be formatted, I tried to resolve the issue manually until it could, or I decided I could not resolve it for reasons.

This was a quick pass. Not meant to be definitive.

# frozen_string_literal: true

require 'tempfile'

filename = ARGV[0]
contents = File.read(filename)

contents.gsub!(/(?<=```go\n)(.*?)(?=```\n)/mu) do |original_code_block|
  code_block = original_code_block

  unless code_block.match(/^package \w+$/)
    # puts '  šŸš§ adding default `package books_test`'
    code_block = "package books_test\n#{code_block}"
  end

  filler = /\s+(\.\.\.|\?\?\?)\s+/
  if code_block.match(filler)
    # puts '  šŸš§ cleaning up filler'
    code_block = code_block.gsub(filler, '/* \1 */')
  end

  file = Tempfile.new(%w[main go])
  file.write(code_block)
  file.close

  skipper = /INVALID|is equivalent to/
  if original_code_block.count("\n") <= 3 || original_code_block.match(skipper)
    # puts '  āœ… valid go'
    original_code_block
  elsif system("gofmt -w #{file.path}")
    # puts '  āœ… valid go'
    File.read(file.path)
  else
    puts code_block
    puts '  šŸ”“ could not format file invalid go'
    original_code_block
  end
end

File.write('errors.md', contents)
onsi commented 12 months ago

hey - I appreciate the time and thought put into this, @jtarchie - but I'm a bit torn. Some thoughts:

But go fmting the examples would be super useful. And I think we can solve for the horizontal space issue via css. So, if you're up for rerunning your script but without the package books_test but with the go fmt stuff (and update the CSS if you're up for it) that would be super valuable.

Thoughts?

jtarchie commented 11 months ago

Thanks for the feedback, not ignoring, just on vacation. šŸ˜„

I will explore your proposal here. Any leads on where the CSS I need to edit would be?

onsi commented 11 months ago

No worries, obviously! Hope vacation's going/gone well :)

I think it probably goes here somewhere:

https://github.com/onsi/ginkgo/blob/74bbd6552ca29d47cf6c828aac38d42fbd912bd9/docs/css/layout.css#L62-L80