<commit_before>require 'spy'
require 'webmock/minitest'
require_relative '../constants'

module Minitest
  #
  # Custom assertions to facilitate CLI command testing
  #
  module Assertions
    def assert_gets(api_path, headers, &block)
      stub_request(:get, api_path)
        .with(headers: headers)
        .to_return(body: DUMMY_RESPONSE, status: 200)
      yield block
      assert_requested(:get, api_path, headers: headers)
    end

    def assert_posts(api_path, headers, payload, &block)
      stub_request(:post, api_path)
        .with(body: payload, headers: headers)
        .to_return(body: DUMMY_RESPONSE, status: 200)
      yield block
      assert_requested(:post, api_path, headers: headers)
    end

    def assert_puts(api_path, headers, _payload, &block)
      stub_request(:put, api_path)
        .with(headers: headers)
        .to_return(body: DUMMY_RESPONSE, status: 200)
      yield block
      assert_requested(:put, api_path, headers: headers)
    end

    def assert_deletes(api_path, headers, &block)
      stub_request(:delete, api_path)
        .with(headers: headers)
        .to_return(body: DUMMY_RESPONSE, status: 200)
      yield block
      assert_requested(:delete, api_path, headers: headers)
    end

    # Don't bother testing permutations for this
    #
    def assert_invalid_id(command)
      out, err = capture_io do
        assert_raises(SystemExit) { wf.new("#{cmd_word} #{command}".split) }
      end

      assert_match(/is not a valid \w+ ID.$/, err)
      assert_empty(out)
    end

    def assert_usage(command)
      out, err = capture_io do
        assert_raises(SystemExit) { wf.new("#{cmd_word} #{command}".split) }
      end

      assert_match(/^Usage:\n  wf #{cmd_word}/, err)
      assert_empty(out)
    end

    def assert_abort_on_missing_creds(command)
      out, err = capture_io do
        assert_raises(SystemExit) do
          wf.new("#{cmd_word} #{command} --config /nofile".split)
        end
      end

      assert_equal("Configuration file '/nofile' not found.\n", err)
      assert_empty(out)
    end

    def assert_exits_with(command, message)
      out, err = capture_io do
        assert_raises(SystemExit) do
          wf.new("#{cmd_word} #{command}".split)
        end
      end

      assert_equal(message, err.strip)
      assert_empty(out)
    end

    def assert_cannot_noop(command)
      out, err = capture_io do<commit_msg>make args consistent<commit_after>require 'spy'
require 'webmock/minitest'
require_relative '../constants'

module Minitest
  #
  # Custom assertions to facilitate CLI command testing
  #
  module Assertions
    def assert_gets(api_path, headers, &block)
      stub_request(:get, api_path)
        .with(headers: headers)
        .to_return(body: DUMMY_RESPONSE, status: 200)
      yield block
      assert_requested(:get, api_path, headers: headers)
    end

    def assert_posts(api_path, headers, payload, &block)
      stub_request(:post, api_path)
        .with(body: payload, headers: headers)
        .to_return(body: DUMMY_RESPONSE, status: 200)
      yield block
      assert_requested(:post, api_path, headers: headers)
    end

    def assert_puts(api_path, headers, _payload, &block)
      stub_request(:put, api_path)
        .with(headers: headers)
        .to_return(body: DUMMY_RESPONSE, status: 200)
      yield block
      assert_requested(:put, api_path, headers: headers)
    end

    def assert_deletes(api_path, headers, &block)
      stub_request(:delete, api_path)
        .with(headers: headers)
        .to_return(body: DUMMY_RESPONSE, status: 200)
      yield block
      assert_requested(:delete, api_path, headers: headers)
    end

    # Don't bother testing permutations for this
    #
    def assert_invalid_id(command)
      out, err = capture_io do
        assert_raises(SystemExit) { wf.new("#{cmd_word} #{command}".split) }
      end

      assert_match(/is not a valid \w+ ID.$/, err)
      assert_empty(out)
    end

    def assert_usage(command)
      out, err = capture_io do
        assert_raises(SystemExit) { wf.new("#{cmd_word} #{command}".split) }
      end

      assert_match(/^Usage:\n  wf #{cmd_word}/, err)
      assert_empty(out)
    end

    def assert_abort_on_missing_creds(command)
      out, err = capture_io do
        assert_raises(SystemExit) do
          wf.new("#{cmd_word} #{command} --config /nofile".split)
        end
      end

      assert_equal("Configuration file '/nofile' not found.\n", err)
      assert_empty(out)
    end

    def assert_exits_with(message, command)
      out, err = capture_io do
        assert_raises(SystemExit) do
          wf.new("#{cmd_word} #{command}".split)
        end
      end

      assert_equal(message, err.strip)
      assert_empty(out)
    end

    def assert_cannot_noop(command)
      out, err = capture_io do
{'fraction_non_alphanumeric': 0.10312694139573411, 'fraction_numerical': 0.004969973079312487, 'mean_word_length': 2.800157356412274, 'pattern_counts': {'":': 0, '<': 3, '<?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': '4873959', 'n_tokens_mistral': 1621, 'n_tokens_neox': 1564, 'n_words': 367}