<commit_before>require_relative 'setup_tests'
require 'open3'
require 'set'
require 'fileutils'

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

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

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

  def test_show_alias_processing_file
    result = run_cli_with_input("-a -r MD002", "## header2")<commit_msg>Add tests for json output (#288)<commit_after>require_relative 'setup_tests'
require 'open3'
require 'set'
require 'fileutils'
require 'json'

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

  def test_json_output
    result = run_cli_with_input("-j", "# header")
    assert_ran_ok(result)
    assert_equal("[]\n", result[:stdout])
  end

  def test_json_output_with_matches
    result = run_cli_with_input("-j -r MD002", "## header2")
    assert_equal(1, result[:status])
    assert_equal("", result[:stderr])
    d = JSON.parse(result[:stdout])
    assert_match(d[0]["rule"], "MD002")
  end

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

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

  def test_show_alias_processing_file
    result = run_cli_with_input("-a -r MD002", "## header2")
{'fraction_non_alphanumeric': 0.13451917732073373, 'fraction_numerical': 0.016675931072818232, 'mean_word_length': 4.538461538461538, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2922674', 'n_tokens_mistral': 644, 'n_tokens_neox': 623, 'n_words': 121}