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
+20 -8
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|
@@ -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'
@@ -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)
+29 -29
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,
@@ -32,9 +10,12 @@ def load_script(**args)
: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'))
@@ -45,7 +26,6 @@ def load_script(**args)
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
@@ -54,10 +34,31 @@ unless ENV['CI']
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
@@ -82,5 +83,4 @@ unless ENV['CI']
FileUtils.rm_rf(@path) FileUtils.rm_rf(@path)
end end
end end
end end