Skip to content

Commit

Permalink
support newline between headers expression and sha256: annotation
Browse files Browse the repository at this point in the history
Alternative to #12, thanks @ckumar1
  • Loading branch information
timbertson committed Jul 28, 2021
1 parent f53b604 commit 58fb84e
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 5 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ fix:
maintenance/fix

test:
test/test-fix.rb
test/test-bump.rb
test/test-render.rb
test/test-bootstrap.rb
Expand Down
16 changes: 11 additions & 5 deletions maintenance/fix
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ def run(cmd, opts={})
end
end

def fix_imports_in_text(contents)
contents.gsub(/(http[^ ]+ using) ([.\\\/~][^ \n]+)\s+(sha256:)/m) do |x|
match = Regexp.last_match
"#{match[1]} (#{match[2]}) #{match[3]}"
end
end

def main
require 'optparse'
actions = []
Expand Down Expand Up @@ -76,10 +83,7 @@ def main
if options[:fix_imports]
# workaround https://github.com/dhall-lang/dhall-haskell/issues/1672
contents = File.read(path)
fixed = contents.gsub(/(http[^ ]+ using) ([.\\\/~][^ ]+) (sha256:[^ $])/) do |x|
match = Regexp.last_match
"#{match[1]} (#{match[2]}) #{match[3]}"
end
fixed = fix_imports_in_text(contents)
if fixed != contents
File.write(path, fixed)
end
Expand Down Expand Up @@ -111,4 +115,6 @@ def main
end
end

main
if $PROGRAM_NAME == __FILE__
main()
end
19 changes: 19 additions & 0 deletions test/test-fix.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/usr/bin/env ruby
require_relative 'lib/utest'

load "maintenance/fix"

test("fix imports") do
[
{
src: "https://example.com using ~/headers.dhall sha256:1234",
fix: "https://example.com using (~/headers.dhall) sha256:1234",
},
{
src: "https://example.com using ./headers.dhall\n sha256:1234",
fix: "https://example.com using (./headers.dhall) sha256:1234",
},
].each do |test_case|
assert_equal(fix_imports_in_text(test_case.fetch(:src)), test_case.fetch(:fix))
end
end

0 comments on commit 58fb84e

Please sign in to comment.