Skip to content

Commit

Permalink
Merge pull request #129 from deivid-rodriguez/issue_126
Browse files Browse the repository at this point in the history
Fixes #126
  • Loading branch information
mivok committed Apr 6, 2016
2 parents a59db54 + fa435e1 commit dfa943c
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 35 deletions.
12 changes: 6 additions & 6 deletions lib/mdl/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -147,15 +147,15 @@ def self.toggle_list(parts, to_sym=false)
end

def self.probe_config_file(path)
# Probe up only for plain filenames
if path != File.basename(path)
return File.expand_path(path)
end
expanded_path = File.expand_path(path)
return expanded_path if File.exist?(expanded_path)

# Look for a file up from the working dir
Pathname.new(path).ascend do |p|
Pathname.new(expanded_path).ascend do |p|
next unless p.directory?

config_file = p.join(CONFIG_FILE)
return config_file if File.exist?(CONFIG_FILE)
return config_file if File.exist?(config_file)
end
nil
end
Expand Down
87 changes: 58 additions & 29 deletions test/test_cli.rb
Original file line number Diff line number Diff line change
@@ -1,20 +1,33 @@
require_relative 'setup_tests'
require 'open3'
require 'set'
require 'fileutils'

class TestCli < Minitest::Test
def run_cli(args, stdin = "", mdlrc="default_mdlrc")
mdl_script = File.expand_path("../../bin/mdl", __FILE__)
# Load the mdlrc file from the text/fixtures/ directory
mdlrc = File.expand_path("../fixtures/#{mdlrc}", __FILE__)
def run_cli_with_rc_flag(args, stdin = "", mdlrc="default_mdlrc")
run_cli("bundle exec #{mdl_script} -c #{fixture_rc(mdlrc)} #{args}", stdin)
end

def run_cli_without_rc_flag(args, stdin = "")
run_cli("bundle exec #{mdl_script} #{args}", stdin)
end

def run_cli(command, stdin)
result = {}
result[:stdout], result[:stderr], result[:status] = \
Open3.capture3(*(%W{bundle exec #{mdl_script} -c #{mdlrc}} + args.split),
:stdin_data => stdin)
Open3.capture3(*command.split, :stdin_data => stdin)
result[:status] = result[:status].exitstatus
result
end

def mdl_script
File.expand_path("../../bin/mdl", __FILE__)
end

def fixture_rc(filename)
File.expand_path("../fixtures/#{filename}", __FILE__)
end

def assert_rules_enabled(result, rules, only_these_rules=false)
# Asserts that the given rules are enabled given the output of mdl -l
# If only_these_rules is set, then it asserts that the given rules and no
Expand Down Expand Up @@ -47,38 +60,38 @@ def assert_ran_ok(result)
end

def test_help_text
result = run_cli("--help")
result = run_cli_with_rc_flag("--help")
assert_match(/Usage: \S+ \[options\]/, result[:stdout])
assert_equal(0, result[:status])
end

def test_default_ruleset_loading
result = run_cli("-l")
result = run_cli_with_rc_flag("-l")
assert_ran_ok(result)
assert_rules_enabled(result, ["MD001"])
end

def test_show_alias_rule_list
result = run_cli("-al")
result = run_cli_with_rc_flag("-al")
assert_ran_ok(result)
assert_rules_enabled(result, ["header-increment"])
end

def test_show_alias_processing_file
result = run_cli("-a -r MD002", "## header2")
result = run_cli_with_rc_flag("-a -r MD002", "## header2")
assert_equal(1, result[:status])
assert_equal("", result[:stderr])
assert_match(/^\(stdin\):1: first-header-h1/, result[:stdout])
end

def test_skipping_default_ruleset_loading
result = run_cli("-ld")
result = run_cli_with_rc_flag("-ld")
assert_rules_enabled(result, [], true)
end

def test_custom_ruleset_loading
my_ruleset = File.expand_path("../fixtures/my_ruleset.rb", __FILE__)
result = run_cli("-ldu #{my_ruleset}")
result = run_cli_with_rc_flag("-ldu #{my_ruleset}")
assert_rules_enabled(result, ["MY001"], true)
assert_ran_ok(result)
end
Expand All @@ -87,21 +100,21 @@ def test_show_alias_rule_without_alias
# Tests that when -a is given, but the rule doesn't have an alias, it
# prints the rule ID instead.
my_ruleset = File.expand_path("../fixtures/my_ruleset.rb", __FILE__)
result = run_cli("-ladu #{my_ruleset}")
result = run_cli_with_rc_flag("-ladu #{my_ruleset}")
assert_rules_enabled(result, ["MY001"], true)
assert_ran_ok(result)
end

def test_custom_ruleset_processing_success
my_ruleset = File.expand_path("../fixtures/my_ruleset.rb", __FILE__)
result = run_cli("-du #{my_ruleset}", "Hello World")
result = run_cli_with_rc_flag("-du #{my_ruleset}", "Hello World")
assert_equal("", result[:stdout])
assert_ran_ok(result)
end

def test_custom_ruleset_processing_failure
my_ruleset = File.expand_path("../fixtures/my_ruleset.rb", __FILE__)
result = run_cli("-du #{my_ruleset}", "Goodbye world")
result = run_cli_with_rc_flag("-du #{my_ruleset}", "Goodbye world")
assert_equal(1, result[:status])
assert_match(/^\(stdin\):1: MY001/, result[:stdout])
assert_equal("", result[:stderr])
Expand All @@ -111,94 +124,110 @@ def test_custom_ruleset_processing_failure_with_show_alias
# The custom rule doesn't have an alias, so the output should be identical
# to that without show_alias enabled.
my_ruleset = File.expand_path("../fixtures/my_ruleset.rb", __FILE__)
result = run_cli("-dau #{my_ruleset}", "Goodbye world")
result = run_cli_with_rc_flag("-dau #{my_ruleset}", "Goodbye world")
assert_equal(1, result[:status])
assert_match(/^\(stdin\):1: MY001/, result[:stdout])
assert_equal("", result[:stderr])
end

def test_custom_ruleset_loading_with_default
my_ruleset = File.expand_path("../fixtures/my_ruleset.rb", __FILE__)
result = run_cli("-lu #{my_ruleset}")
result = run_cli_with_rc_flag("-lu #{my_ruleset}")
assert_rules_enabled(result, ["MD001", "MY001"])
assert_ran_ok(result)
end

def test_rule_inclusion_cli
result = run_cli("-r MD001 -l")
result = run_cli_with_rc_flag("-r MD001 -l")
assert_rules_enabled(result, ["MD001"], true)
assert_ran_ok(result)
end

def test_rule_exclusion_cli
result = run_cli("-r ~MD001 -l")
result = run_cli_with_rc_flag("-r ~MD001 -l")
assert_rules_disabled(result, ["MD001"])
assert_ran_ok(result)
end

def test_rule_inclusion_with_exclusion_cli
result = run_cli("-r ~MD001,MD039 -l")
result = run_cli_with_rc_flag("-r ~MD001,MD039 -l")
assert_rules_enabled(result, ["MD039"], true)
assert_ran_ok(result)
end

def test_tag_inclusion_cli
result = run_cli("-t headers -l")
result = run_cli_with_rc_flag("-t headers -l")
assert_rules_enabled(result, ["MD001", "MD002", "MD003"])
assert_rules_disabled(result, ["MD004", "MD005", "MD006"])
assert_ran_ok(result)
end

def test_tag_exclusion_cli
result = run_cli("-t ~headers -l")
result = run_cli_with_rc_flag("-t ~headers -l")
assert_ran_ok(result)
assert_rules_disabled(result, ["MD001", "MD002", "MD003"])
assert_rules_enabled(result, ["MD004", "MD005", "MD006"])
end

def test_rule_inclusion_config
result = run_cli("-l", "", "mdlrc_enable_rules")
result = run_cli_with_rc_flag("-l", "", "mdlrc_enable_rules")
assert_ran_ok(result)
assert_rules_enabled(result, ["MD001", "MD002"], true)
end

def test_rule_exclusion_config
result = run_cli("-l", "", "mdlrc_disable_rules")
result = run_cli_with_rc_flag("-l", "", "mdlrc_disable_rules")
assert_ran_ok(result)
assert_rules_disabled(result, ["MD001", "MD002"])
assert_rules_enabled(result, ["MD003", "MD004"])
end

def test_mdlrc_loading_from_current_dir_by_default
with_mdlrc("mdlrc_disable_rules") do
result = run_cli_without_rc_flag("-l", "")
assert_ran_ok(result)
assert_rules_disabled(result, ["MD001", "MD002"])
assert_rules_enabled(result, ["MD003", "MD004"])
end
end

def with_mdlrc(filename)
FileUtils.cp(fixture_rc(filename), ".mdlrc")
yield
ensure
File.delete(".mdlrc")
end

def test_tag_inclusion_config
result = run_cli("-l", "", "mdlrc_enable_tags")
result = run_cli_with_rc_flag("-l", "", "mdlrc_enable_tags")
assert_ran_ok(result)
assert_rules_enabled(result, ["MD001", "MD002", "MD009", "MD010"])
assert_rules_disabled(result, ["MD004", "MD005"])
end

def test_tag_exclusion_config
result = run_cli("-l", "", "mdlrc_disable_tags")
result = run_cli_with_rc_flag("-l", "", "mdlrc_disable_tags")
assert_ran_ok(result)
assert_rules_enabled(result, ["MD004", "MD030", "MD032"])
assert_rules_disabled(result, ["MD001", "MD005"])
end

def test_rule_inclusion_alias_cli
result = run_cli("-l -r header-increment")
result = run_cli_with_rc_flag("-l -r header-increment")
assert_ran_ok(result)
assert_rules_enabled(result, ["MD001"], true)
end

def test_rule_exclusion_alias_cli
result = run_cli("-l -r ~header-increment")
result = run_cli_with_rc_flag("-l -r ~header-increment")
assert_ran_ok(result)
assert_rules_disabled(result, ["MD001"])
assert_rules_enabled(result, ["MD002"])
end

def test_directory_scanning
path = File.expand_path("./fixtures/dir_with_md_and_markdown", File.dirname(__FILE__))
result = run_cli("#{path}")
result = run_cli_with_rc_flag("#{path}")
files_with_issues = result[:stdout].split("\n").map { |l| l.split(":")[0] }.sort
assert_equal(files_with_issues, ["#{path}/bar.markdown", "#{path}/foo.md"])
end
Expand Down

0 comments on commit dfa943c

Please sign in to comment.