Sha256: d39515cf05301317516e5c41540df6ad2217f2e3be38d84f164d0c73dc81d2ef

Contents?: true

Size: 1.12 KB

Versions: 1

Compression:

Stored size: 1.12 KB

Contents

# frozen_string_literal: true

describe Nanoc::CLI do
  let(:all_commands) do
    ObjectSpace.each_object(Cri::Command)
  end

  let(:exceptions) do
    # FIXME: Get rid of these exceptions by Nanoc 5.0
    [
      ['deploy', ['C']],
      ['help', ['v']],
      ['check', ['d']],
    ]
  end

  def ancestors_of_command(command)
    if command.is_a?(Cri::Command)
      [command] + ancestors_of_command(command.supercommand)
    else
      []
    end
  end

  def short_options_for_command(command)
    ancestors = ancestors_of_command(command)
    ancestors.flat_map { |a| a.option_definitions.to_a.map { |od| od[:short] } }.compact
  end

  it 'has no commands that have conflicting options' do
    all_commands.each do |command|
      short_options = short_options_for_command(command)

      duplicate_options = short_options.select { |o| short_options.count(o) > 1 }.uniq

      next if exceptions.include?([command.name, duplicate_options])

      expect(duplicate_options).to(
        be_empty,
        "The #{command.name} command’s option shorthands #{duplicate_options.uniq} are used by multiple options",
      )
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
nanoc-4.8.2 spec/nanoc/cli_spec.rb