Update migration script to test run warnings

When running the entire test suite, many warnings would be output:

/Users/bw/Projects/gollum/test/test_migrate.rb:37: warning: already initialized constant HYPHENATE
/Users/bw/Projects/gollum/test/test_migrate.rb:37: warning: previous definition of HYPHENATE was here
/Users/bw/Projects/gollum/test/test_migrate.rb:37: warning: already initialized constant PAGE_FILE_DIR
/Users/bw/Projects/gollum/test/test_migrate.rb:37: warning: previous definition of PAGE_FILE_DIR was here
/Users/bw/Projects/gollum/bin/gollum-migrate-tags:91: warning: already initialized constant REPO
/Users/bw/Projects/gollum/bin/gollum-migrate-tags:91: warning: previous definition of REPO was here
/Users/bw/Projects/gollum/bin/gollum-migrate-tags:236: warning: already initialized constant TREE
/Users/bw/Projects/gollum/bin/gollum-migrate-tags:236: warning: previous definition of TREE was here
/Users/bw/Projects/gollum/test/test_migrate.rb:37: warning: already initialized constant PAGE_FILE_DIR
/Users/bw/Projects/gollum/test/test_migrate.rb:37: warning: previous definition of PAGE_FILE_DIR was here
/Users/bw/Projects/gollum/bin/gollum-migrate-tags:91: warning: already initialized constant REPO
/Users/bw/Projects/gollum/bin/gollum-migrate-tags:91: warning: previous definition of REPO was here
/Users/bw/Projects/gollum/bin/gollum-migrate-tags:236: warning: already initialized constant TREE
/Users/bw/Projects/gollum/bin/gollum-migrate-tags:236: warning: previous definition of TREE was here

While it's unlikely that end users would ever see these warnings, as
they'd only be running the migration script once, they will always be
shown in our local test runs and CI run output.

So instead of using constants in our migration script, I change the
script to use class variables instead. This should not effect the
functionality of the migration script whatsoever.
This commit is contained in:
benjamin wil
2022-01-02 14:15:36 -08:00
parent f5c8af9904
commit 0b18f475f0
2 changed files with 82 additions and 70 deletions
+40 -28
View File
@@ -11,8 +11,11 @@ migrate_options = {
:hyphenate => true :hyphenate => true
} }
def setting(const) def setting(variable_name)
Object.const_defined?(const.upcase) && Object.const_get(const.upcase) class_variable_name = :"@@#{variable_name.to_s}"
Object.class_variable_defined?(class_variable_name) &&
Object.class_variable_get(class_variable_name)
end end
opts = OptionParser.new do |opts| opts = OptionParser.new do |opts|
@@ -25,7 +28,7 @@ It finds and repairs Gollum link tags that no longer work under 5.x for three re
* 5.x wiki internal links are no longer 'global'. * 5.x wiki internal links are no longer 'global'.
* NB: you can use the --lenient-tag-lookup option in gollum >= 5.x to enable 4.x-backwards compatible tags. * NB: you can use the --lenient-tag-lookup option in gollum >= 5.x to enable 4.x-backwards compatible tags.
See https://github.com/gollum/gollum/wiki/5.0-release-notes#filename-handling for more information. See https://github.com/gollum/gollum/wiki/5.0-release-notes#filename-handling for more information.
Usage of this script comes without any warranty. Usage of this script comes without any warranty.
@@ -38,7 +41,7 @@ You can use the --page-file-dir and --config options as you would normally with
Requires a non-bare repository. Recommended usage: Requires a non-bare repository. Recommended usage:
1. Clone your wiki's repository to create a backup. 1. Clone your wiki's repository to create a backup.
2. Run this script on your cloned repo. 2. Run this script on your cloned repo.
3. If all looks sane, run the script with the --write option. This will overwrite files in your working directory, but not commit the changes, so you have time to review them. 3. If all looks sane, run the script with the --write option. This will overwrite files in your working directory, but not commit the changes, so you have time to review them.
4. Do a 'git diff' to inspect the changes. 4. Do a 'git diff' to inspect the changes.
5. Commit the changes if all looks sane, and push/pull them back into your original repo. 5. Commit the changes if all looks sane, and push/pull them back into your original repo.
@@ -52,23 +55,23 @@ EOF
opts.on('--page-file-dir [PATH]', 'Specify the subdirectory for all pages. Default: repository root.') do |path| opts.on('--page-file-dir [PATH]', 'Specify the subdirectory for all pages. Default: repository root.') do |path|
wiki_options[:page_file_dir] = path wiki_options[:page_file_dir] = path
end end
opts.on('--prefer-relative-links', 'When specified, will try to replace broken links with relative links (\'[[Foo/Bar]]\' instead of \'[[/Subdir/Foo/Bar]]\') where possible.') do opts.on('--prefer-relative-links', 'When specified, will try to replace broken links with relative links (\'[[Foo/Bar]]\' instead of \'[[/Subdir/Foo/Bar]]\') where possible.') do
migrate_options[:prefer_relative] = true migrate_options[:prefer_relative] = true
end end
opts.on('--hyphenate', 'Default. Repair links that use spaces instead of hyphens: [[Bilbo Baggins]] -> [[Bilbo-Baggins]]') do opts.on('--hyphenate', 'Default. Repair links that use spaces instead of hyphens: [[Bilbo Baggins]] -> [[Bilbo-Baggins]]') do
migrate_options[:hyphenate] = true migrate_options[:hyphenate] = true
end end
opts.on('--no-hyphenate', 'Turn off the --hyphenate option.') do opts.on('--no-hyphenate', 'Turn off the --hyphenate option.') do
migrate_options[:hyphenate] = false migrate_options[:hyphenate] = false
end end
opts.on('--run-silent', 'Don\'t output anything.') do opts.on('--run-silent', 'Don\'t output anything.') do
migrate_options[:run_silent] = true migrate_options[:run_silent] = true
end end
opts.on('--write', 'No dry run: actually perform the substitutions.') do opts.on('--write', 'No dry run: actually perform the substitutions.') do
migrate_options[:no_dry_run] = true migrate_options[:no_dry_run] = true
end end
@@ -78,8 +81,11 @@ end
begin begin
opts.parse! opts.parse!
migrate_options.each do |setting, value| migrate_options.each do |setting, value|
const = setting.to_s.upcase variable_name = :"@@#{setting.to_s}"
Object.const_set(const, value) unless Object.const_defined?(const)
unless Object.class_variable_defined?(variable_name)
Object.class_variable_set(variable_name, value)
end
end end
wiki_options[:page_file_dir] = setting(:page_file_dir) ? setting(:page_file_dir) : wiki_options[:page_file_dir] # Allow settings :page_file_dir through PAGE_FILE_DIR constant. wiki_options[:page_file_dir] = setting(:page_file_dir) ? setting(:page_file_dir) : wiki_options[:page_file_dir] # Allow settings :page_file_dir through PAGE_FILE_DIR constant.
rescue OptionParser::InvalidOption rescue OptionParser::InvalidOption
@@ -88,7 +94,7 @@ rescue OptionParser::InvalidOption
exit exit
end end
REPO = ARGV[0] || Dir.pwd wiki_directory = ARGV[0] || Dir.pwd
require 'gollum-lib' require 'gollum-lib'
@@ -98,7 +104,7 @@ if cfg = options[:config]
cfg = File.join(Dir.getwd, cfg) unless cfg.slice(0) == File::SEPARATOR cfg = File.join(Dir.getwd, cfg) unless cfg.slice(0) == File::SEPARATOR
require cfg require cfg
end end
class Gollum::Filter::CodeMigrator < Gollum::Filter::Code class Gollum::Filter::CodeMigrator < Gollum::Filter::Code
def extract(data) def extract(data)
case @markup.format case @markup.format
@@ -126,7 +132,7 @@ class Gollum::Filter::CodeMigrator < Gollum::Filter::Code
next '' if m_end.length < m_start.length next '' if m_end.length < m_start.length
lang = m_lang ? m_lang.strip.split.first : nil lang = m_lang ? m_lang.strip.split.first : nil
cache_codeblock($~.to_s) cache_codeblock($~.to_s)
end end
end end
data.gsub!(/^([ ]{0,3})``` ?([^\r\n]+)?\r?\n(.+?)\r?\n[ ]{0,3}```[ \t]*\r?$/m) do data.gsub!(/^([ ]{0,3})``` ?([^\r\n]+)?\r?\n(.+?)\r?\n[ ]{0,3}```[ \t]*\r?$/m) do
@@ -134,7 +140,7 @@ class Gollum::Filter::CodeMigrator < Gollum::Filter::Code
end end
data data
end end
def process(data) def process(data)
return data if data.nil? || data.size.zero? || @map.size.zero? return data if data.nil? || data.size.zero? || @map.size.zero?
@map.each do |id, block| ## Just put the code blocks back in verbatim @map.each do |id, block| ## Just put the code blocks back in verbatim
@@ -142,23 +148,23 @@ class Gollum::Filter::CodeMigrator < Gollum::Filter::Code
end end
data data
end end
def cache_codeblock(block) def cache_codeblock(block)
id = "#{open_pattern}#{Digest::SHA1.hexdigest(block)}#{close_pattern}" id = "#{open_pattern}#{Digest::SHA1.hexdigest(block)}#{close_pattern}"
@map[id] = block @map[id] = block
id id
end end
end end
class ::Gollum::Filter::TagMigrator < Gollum::Filter::Tags class ::Gollum::Filter::TagMigrator < Gollum::Filter::Tags
def process_tag(tag) def process_tag(tag)
link_part, extra = parse_tag_parts(tag) link_part, extra = parse_tag_parts(tag)
orig_tag = %{[[#{tag}]]} orig_tag = %{[[#{tag}]]}
return orig_tag if link_part.nil? return orig_tag if link_part.nil?
img_args = extra ? [extra, link_part] : [link_part] img_args = extra ? [extra, link_part] : [link_part]
mime = MIME::Types.type_for(::File.extname(img_args.first.to_s)).first mime = MIME::Types.type_for(::File.extname(img_args.first.to_s)).first
# For any kind of tag other than an internal link: just return the tag. # For any kind of tag other than an internal link: just return the tag.
if tag =~ /^_TOC_/ || link_part =~ /^_$/ || link_part =~ /^#{INCLUDE_TAG}/ || (mime && mime.content_type =~ /^image/) || process_external_link_tag(link_part, extra) || process_file_link_tag(link_part, extra) if tag =~ /^_TOC_/ || link_part =~ /^_$/ || link_part =~ /^#{INCLUDE_TAG}/ || (mime && mime.content_type =~ /^image/) || process_external_link_tag(link_part, extra) || process_file_link_tag(link_part, extra)
return orig_tag return orig_tag
@@ -168,7 +174,7 @@ class ::Gollum::Filter::TagMigrator < Gollum::Filter::Tags
link = link_part link = link_part
page = find_page_or_file_from_path(link) page = find_page_or_file_from_path(link)
anchor = nil anchor = nil
if page.nil? # No match yet, now try finding the page with anchor removed if page.nil? # No match yet, now try finding the page with anchor removed
if pos = link.rindex('#') if pos = link.rindex('#')
anchor = link[pos..-1] anchor = link[pos..-1]
@@ -203,12 +209,12 @@ class ::Gollum::Filter::TagMigrator < Gollum::Filter::Tags
pick = possibles.first pick = possibles.first
return tag_for_pick(pick, orig_tag, extra, anchor, @markup.page.path) return tag_for_pick(pick, orig_tag, extra, anchor, @markup.page.path)
end end
end end
end end
private private
def tag_for_pick(pick, orig_tag, extra, anchor, linking_page_path) def tag_for_pick(pick, orig_tag, extra, anchor, linking_page_path)
pick = if setting(:prefer_relative) pick = if setting(:prefer_relative)
overlapping_path = Pathname.new(linking_page_path).dirname.to_s overlapping_path = Pathname.new(linking_page_path).dirname.to_s
@@ -220,7 +226,7 @@ class ::Gollum::Filter::TagMigrator < Gollum::Filter::Tags
end end
new_tag = extra.nil? ? %{[[#{pick}#{anchor}]]} : %{[[#{extra}|#{pick}#{anchor}]]} new_tag = extra.nil? ? %{[[#{pick}#{anchor}]]} : %{[[#{extra}|#{pick}#{anchor}]]}
log(:info, "#{@markup.page.path}: Changing #{orig_tag} -> #{new_tag}") log(:info, "#{@markup.page.path}: Changing #{orig_tag} -> #{new_tag}")
new_tag new_tag
end end
end end
@@ -232,8 +238,12 @@ end
filter_chain = [:PlainTextMigrator, :CodeMigrator, :TagMigrator] filter_chain = [:PlainTextMigrator, :CodeMigrator, :TagMigrator]
wiki = ::Gollum::Wiki.new(REPO, wiki_options.merge({:filter_chain => filter_chain})) wiki = ::Gollum::Wiki.new(wiki_directory, wiki_options.merge({:filter_chain => filter_chain}))
TREE = wiki.tree_list(wiki.ref, true, true).map {|file| ::File.join('/', file.path)}
Object.class_variable_set(
:"@@wiki_tree",
wiki.tree_list(wiki.ref, true, true).map {|file| ::File.join('/', file.path)}
)
def find_linked(link) def find_linked(link)
link.gsub!(' ', '-') if setting(:hyphenate) # Match paths containing dashes instead of spaces link.gsub!(' ', '-') if setting(:hyphenate) # Match paths containing dashes instead of spaces
@@ -243,7 +253,9 @@ def find_linked(link)
test_path = ::File.extname(link).empty? ? /#{link}\..+/ : link test_path = ::File.extname(link).empty? ? /#{link}\..+/ : link
# Select pages from the wiki whose path =~ 'Foo/Bar/Samwi.*' # Select pages from the wiki whose path =~ 'Foo/Bar/Samwi.*'
# Match case-insenstively to mimic 4.x behavior! # Match case-insenstively to mimic 4.x behavior!
TREE.select {|path| path =~ /^\/(.*\/)?#{test_path}/i} Object.class_variable_get(:"@@wiki_tree").select { |path|
path =~ /^\/(.*\/)?#{test_path}/i
}
end end
def log(kind, msg = nil) def log(kind, msg = nil)
@@ -268,4 +280,4 @@ wiki.pages.each do |page|
f.close f.close
end end
log(:none, '====') log(:none, '====')
end end
+42 -42
View File
@@ -1,28 +1,6 @@
# ~*~ encoding: utf-8 ~*~ # ~*~ encoding: utf-8 ~*~
require File.expand_path(File.join(File.dirname(__FILE__), 'helper')) require File.expand_path(File.join(File.dirname(__FILE__), 'helper'))
# Original contents of Subdir/Foo.md:
# waa
# [[Samwi]]
# [[samwise gamgee.mediaWiki]]
# [[Samwise Gamgee.mediawiki]]
# [[Samwise Gamgee]]
# [[Test|Samwise Gamgee#Anchor]]
# [[Waaa|Test]]
# [[Zaa]]
# Contents of Subdir/Foo.md after successful tag migration
result = <<EOF
waa
[[Samwi]]
[[/Samwise Gamgee.mediawiki]]
[[/Samwise Gamgee.mediawiki]]
[[/Samwise Gamgee.md]]
[[Test|/Samwise Gamgee.md#Anchor]]
[[Waaa|/Bar/Test.md]]
[[Subsub/Zaa.md]]
EOF
def load_script(**args) def load_script(**args)
settings = { settings = {
:run_silent => true, :run_silent => true,
@@ -31,47 +9,70 @@ def load_script(**args)
:hyphenate => false, :hyphenate => false,
:page_file_dir => nil, :page_file_dir => nil,
}.merge(args) }.merge(args)
settings.each do |const, val| settings.each do |setting, val|
const_name = const.to_s.upcase variable_name = :"@@#{setting.to_s}"
Object.const_set(const_name, val) unless Object.const_defined?(const_name) && Object.const_get(const_name) == val
unless Object.class_variable_defined?(variable_name) && Object.class_variable_get(variable_name) == val
Object.class_variable_set(variable_name, val)
end
end end
script_path = File.expand_path(File.join(File.dirname(__FILE__), '../', 'bin', 'gollum-migrate-tags')) script_path = File.expand_path(File.join(File.dirname(__FILE__), '../', 'bin', 'gollum-migrate-tags'))
Dir.chdir(@path) do Dir.chdir(@path) do
load script_path load script_path
end end
end end
unless ENV['CI'] unless ENV['CI']
context '4.x -> 5.x tag migrator' do context '4.x -> 5.x tag migrator' do
include Rack::Test::Methods include Rack::Test::Methods
setup do setup do
@path = cloned_testpath("examples/lotr_migration.git") @path = cloned_testpath("examples/lotr_migration.git")
end end
test 'repair broken links' do test 'repair broken links' do
# The original contents of Subdir/Foo.md:
#
# waa
# [[Samwi]]
# [[samwise gamgee.mediaWiki]]
# [[Samwise Gamgee.mediawiki]]
# [[Samwise Gamgee]]
# [[Test|Samwise Gamgee#Anchor]]
# [[Waaa|Test]]
# [[Zaa]]
#
# The contents will be updated after running the migration script.
load_script load_script
f = ::File.new(::File.join(@path, 'Subdir/Foo.md'), 'r') file = ::File.new(::File.join(@path, 'Subdir/Foo.md'), 'r')
assert_equal result, f.read assert_equal <<~FILE_CONTENTS, file.read
waa
[[Samwi]]
[[/Samwise Gamgee.mediawiki]]
[[/Samwise Gamgee.mediawiki]]
[[/Samwise Gamgee.md]]
[[Test|/Samwise Gamgee.md#Anchor]]
[[Waaa|/Bar/Test.md]]
[[Subsub/Zaa.md]]
FILE_CONTENTS
end end
test 'change spaced filenames to hyphenated filenames' do test 'change spaced filenames to hyphenated filenames' do
load_script(hyphenate: true) load_script(hyphenate: true)
f = ::File.new(::File.join(@path, 'Home.textile'), 'r') f = ::File.new(::File.join(@path, 'Home.textile'), 'r')
output = f.read output = f.read
assert_equal true, output.include?('[[Bilbo-Baggins.md]]') assert_equal true, output.include?('[[Bilbo-Baggins.md]]')
assert_equal true, output.include?('[[evil|Mordor/Eye-Of-Sauron.md]]') assert_equal true, output.include?('[[evil|Mordor/Eye-Of-Sauron.md]]')
end end
test 'migration with page file dir' do test 'migration with page file dir' do
load_script(page_file_dir: 'Subdir') load_script(page_file_dir: 'Subdir')
f = ::File.new(::File.join(@path, 'Subdir/Foo.md'), 'r') f = ::File.new(::File.join(@path, 'Subdir/Foo.md'), 'r')
output = f.read output = f.read
assert_equal true, output.include?('[[Subsub/Zaa.md]]') assert_equal true, output.include?('[[Subsub/Zaa.md]]')
@@ -82,5 +83,4 @@ unless ENV['CI']
FileUtils.rm_rf(@path) FileUtils.rm_rf(@path)
end end
end end
end
end