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:
+20
-8
@@ -11,8 +11,11 @@ migrate_options = {
|
||||
:hyphenate => true
|
||||
}
|
||||
|
||||
def setting(const)
|
||||
Object.const_defined?(const.upcase) && Object.const_get(const.upcase)
|
||||
def setting(variable_name)
|
||||
class_variable_name = :"@@#{variable_name.to_s}"
|
||||
|
||||
Object.class_variable_defined?(class_variable_name) &&
|
||||
Object.class_variable_get(class_variable_name)
|
||||
end
|
||||
|
||||
opts = OptionParser.new do |opts|
|
||||
@@ -78,8 +81,11 @@ end
|
||||
begin
|
||||
opts.parse!
|
||||
migrate_options.each do |setting, value|
|
||||
const = setting.to_s.upcase
|
||||
Object.const_set(const, value) unless Object.const_defined?(const)
|
||||
variable_name = :"@@#{setting.to_s}"
|
||||
|
||||
unless Object.class_variable_defined?(variable_name)
|
||||
Object.class_variable_set(variable_name, value)
|
||||
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.
|
||||
rescue OptionParser::InvalidOption
|
||||
@@ -88,7 +94,7 @@ rescue OptionParser::InvalidOption
|
||||
exit
|
||||
end
|
||||
|
||||
REPO = ARGV[0] || Dir.pwd
|
||||
wiki_directory = ARGV[0] || Dir.pwd
|
||||
|
||||
require 'gollum-lib'
|
||||
|
||||
@@ -232,8 +238,12 @@ end
|
||||
|
||||
filter_chain = [:PlainTextMigrator, :CodeMigrator, :TagMigrator]
|
||||
|
||||
wiki = ::Gollum::Wiki.new(REPO, wiki_options.merge({:filter_chain => filter_chain}))
|
||||
TREE = wiki.tree_list(wiki.ref, true, true).map {|file| ::File.join('/', file.path)}
|
||||
wiki = ::Gollum::Wiki.new(wiki_directory, wiki_options.merge({:filter_chain => filter_chain}))
|
||||
|
||||
Object.class_variable_set(
|
||||
:"@@wiki_tree",
|
||||
wiki.tree_list(wiki.ref, true, true).map {|file| ::File.join('/', file.path)}
|
||||
)
|
||||
|
||||
def find_linked(link)
|
||||
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
|
||||
# Select pages from the wiki whose path =~ 'Foo/Bar/Samwi.*'
|
||||
# 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
|
||||
|
||||
def log(kind, msg = nil)
|
||||
|
||||
+29
-29
@@ -1,28 +1,6 @@
|
||||
# ~*~ encoding: utf-8 ~*~
|
||||
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)
|
||||
settings = {
|
||||
:run_silent => true,
|
||||
@@ -32,9 +10,12 @@ def load_script(**args)
|
||||
:page_file_dir => nil,
|
||||
}.merge(args)
|
||||
|
||||
settings.each do |const, val|
|
||||
const_name = const.to_s.upcase
|
||||
Object.const_set(const_name, val) unless Object.const_defined?(const_name) && Object.const_get(const_name) == val
|
||||
settings.each do |setting, val|
|
||||
variable_name = :"@@#{setting.to_s}"
|
||||
|
||||
unless Object.class_variable_defined?(variable_name) && Object.class_variable_get(variable_name) == val
|
||||
Object.class_variable_set(variable_name, val)
|
||||
end
|
||||
end
|
||||
|
||||
script_path = File.expand_path(File.join(File.dirname(__FILE__), '../', 'bin', 'gollum-migrate-tags'))
|
||||
@@ -45,7 +26,6 @@ def load_script(**args)
|
||||
end
|
||||
|
||||
unless ENV['CI']
|
||||
|
||||
context '4.x -> 5.x tag migrator' do
|
||||
include Rack::Test::Methods
|
||||
|
||||
@@ -54,10 +34,31 @@ unless ENV['CI']
|
||||
end
|
||||
|
||||
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
|
||||
|
||||
f = ::File.new(::File.join(@path, 'Subdir/Foo.md'), 'r')
|
||||
assert_equal result, f.read
|
||||
file = ::File.new(::File.join(@path, 'Subdir/Foo.md'), 'r')
|
||||
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
|
||||
|
||||
test 'change spaced filenames to hyphenated filenames' do
|
||||
@@ -82,5 +83,4 @@ unless ENV['CI']
|
||||
FileUtils.rm_rf(@path)
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
Reference in New Issue
Block a user