Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consider hyphen and underscore in fluent-plugin-generate arguments #1751

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 24 additions & 8 deletions lib/fluent/command/plugin_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -137,32 +137,36 @@ def user_email
end

def gem_name
"fluent-plugin-#{name}"
"fluent-plugin-#{dash_name}"
end

def plugin_name
underscore_name
end

def class_name
"#{name.capitalize}#{type.capitalize}"
"#{capitalized_name}#{type.capitalize}"
end

def plugin_filename
case type
when "input"
"in_#{name}.rb"
"in_#{underscore_name}.rb"
when "output"
"out_#{name}.rb"
"out_#{underscore_name}.rb"
else
"#{type}_#{name}.rb"
"#{type}_#{underscore_name}.rb"
end
end

def test_filename
case type
when "input"
"test_in_#{name}.rb"
"test_in_#{underscore_name}.rb"
when "output"
"test_out_#{name}.rb"
"test_out_#{underscore_name}.rb"
else
"test_#{type}_#{name}.rb"
"test_#{type}_#{underscore_name}.rb"
end
end

Expand All @@ -179,6 +183,18 @@ def dest_filename(path)
end
end

def capitalized_name
@capitalized_name ||= name.split(/[-_]/).map(&:capitalize).join
end

def underscore_name
@underscore_name ||= name.tr("-", "_")
end

def dash_name
@dash_name ||= name.tr("_", "-")
end

def preamble
@license.preamble(user_name)
end
Expand Down
2 changes: 1 addition & 1 deletion templates/new_gem/lib/fluent/plugin/filter.rb.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ require "fluent/plugin/filter"
module Fluent
module Plugin
class <%= class_name %> < Fluent::Plugin::Filter
Fluent::Plugin.register_filter("<%= name %>", self)
Fluent::Plugin.register_filter("<%= plugin_name %>", self)

def filter(tag, time, record)
end
Expand Down
2 changes: 1 addition & 1 deletion templates/new_gem/lib/fluent/plugin/input.rb.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ require "fluent/plugin/input"
module Fluent
module Plugin
class <%= class_name %> < Fluent::Plugin::Input
Fluent::Plugin.register_input("<%= name %>", self)
Fluent::Plugin.register_input("<%= plugin_name %>", self)
end
end
end
2 changes: 1 addition & 1 deletion templates/new_gem/lib/fluent/plugin/output.rb.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ require "fluent/plugin/output"
module Fluent
module Plugin
class <%= class_name %> < Fluent::Plugin::Output
Fluent::Plugin.register_output("<%= name %>", self)
Fluent::Plugin.register_output("<%= plugin_name %>", self)
end
end
end
2 changes: 1 addition & 1 deletion templates/new_gem/lib/fluent/plugin/parser.rb.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ require "fluent/plugin/parser"
module Fluent
module Plugin
class <%= class_name %> < Fluent::Plugin::Parser
Fluent::Plugin.register_parser("<%= name %>", self)
Fluent::Plugin.register_parser("<%= plugin_name %>", self)
end

def parse(text)
Expand Down
104 changes: 65 additions & 39 deletions test/command/test_plugin_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,51 +16,77 @@ class TestFluentPluginGenerator < Test::Unit::TestCase
FileUtils.rm_rf(TEMP_DIR)
end

data(input: ["input", "in"],
output: ["output", "out"],
filter: ["filter", "filter"],
parser: ["parser", "parser"],
formatter: ["formatter", "formatter"])
test "generate plugin" do |(type, part)|
capture_stdout do
FluentPluginGenerator.new([type, "fake"]).call
sub_test_case "generate plugin" do
data(input: ["input", "in"],
output: ["output", "out"],
filter: ["filter", "filter"],
parser: ["parser", "parser"],
formatter: ["formatter", "formatter"])
test "generate plugin" do |(type, part)|
capture_stdout do
FluentPluginGenerator.new([type, "fake"]).call
end
plugin_base_dir = Pathname("fluent-plugin-fake")
assert { plugin_base_dir.directory? }
expected = [
"fluent-plugin-fake",
"fluent-plugin-fake/Gemfile",
"fluent-plugin-fake/LICENSE",
"fluent-plugin-fake/README.md",
"fluent-plugin-fake/Rakefile",
"fluent-plugin-fake/fluent-plugin-fake.gemspec",
"fluent-plugin-fake/lib",
"fluent-plugin-fake/lib/fluent",
"fluent-plugin-fake/lib/fluent/plugin",
"fluent-plugin-fake/lib/fluent/plugin/#{part}_fake.rb",
"fluent-plugin-fake/test",
"fluent-plugin-fake/test/helper.rb",
"fluent-plugin-fake/test/plugin",
"fluent-plugin-fake/test/plugin/test_#{part}_fake.rb",
]
actual = plugin_base_dir.find.reject {|f| f.fnmatch("*/.git*") }.map(&:to_s).sort
assert_equal(expected, actual)
end
plugin_base_dir = Pathname("fluent-plugin-fake")
assert { plugin_base_dir.directory? }
expected = [
"fluent-plugin-fake",
"fluent-plugin-fake/Gemfile",
"fluent-plugin-fake/LICENSE",
"fluent-plugin-fake/README.md",
"fluent-plugin-fake/Rakefile",
"fluent-plugin-fake/fluent-plugin-fake.gemspec",
"fluent-plugin-fake/lib",
"fluent-plugin-fake/lib/fluent",
"fluent-plugin-fake/lib/fluent/plugin",
"fluent-plugin-fake/lib/fluent/plugin/#{part}_fake.rb",
"fluent-plugin-fake/test",
"fluent-plugin-fake/test/helper.rb",
"fluent-plugin-fake/test/plugin",
"fluent-plugin-fake/test/plugin/test_#{part}_fake.rb",
]
actual = plugin_base_dir.find.reject {|f| f.fnmatch("*/.git*") }.map(&:to_s).sort
assert_equal(expected, actual)
end

test "no license" do
capture_stdout do
FluentPluginGenerator.new(["--no-license", "filter", "fake"]).call
test "no license" do
capture_stdout do
FluentPluginGenerator.new(["--no-license", "filter", "fake"]).call
end
assert { !Pathname("fluent-plugin-fake/LICENSE").exist? }
assert { Pathname("fluent-plugin-fake/Gemfile").exist? }
end

test "unknown license" do
out = capture_stdout do
assert_raise(SystemExit) do
FluentPluginGenerator.new(["--license=unknown", "filter", "fake"]).call
end
end
assert { out.lines.include?("License: unknown\n") }
end
assert { !Pathname("fluent-plugin-fake/LICENSE").exist? }
assert { Pathname("fluent-plugin-fake/Gemfile").exist? }
end

test "unknown license" do
out = capture_stdout do
assert_raise(SystemExit) do
FluentPluginGenerator.new(["--license=unknown", "filter", "fake"]).call
sub_test_case "unify plugin name" do
data("word" => ["fake", "fake"],
"underscore" => ["rewrite_tag_filter", "rewrite_tag_filter"],
"dash" => ["rewrite-tag-filter", "rewrite_tag_filter"])
test "plugin_name" do |(name, plugin_name)|
generator = FluentPluginGenerator.new(["filter", name])
capture_stdout do
generator.call
end
assert_equal(plugin_name, generator.__send__(:plugin_name))
end

data("word" => ["fake", "fluent-plugin-fake"],
"underscore" => ["rewrite_tag_filter", "fluent-plugin-rewrite-tag-filter"],
"dash" => ["rewrite-tag-filter", "fluent-plugin-rewrite-tag-filter"])
test "gem_name" do |(name, gem_name)|
generator = FluentPluginGenerator.new(["output", name])
capture_stdout do
generator.call
end
assert_equal(gem_name, generator.__send__(:gem_name))
end
assert { out.lines.include?("License: unknown\n") }
end
end