command_kit 0.3.0 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.github/workflows/ruby.yml +4 -6
- data/.rubocop.yml +13 -0
- data/ChangeLog.md +18 -0
- data/Gemfile +2 -0
- data/LICENSE.txt +1 -1
- data/README.md +18 -9
- data/command_kit.gemspec +0 -1
- data/examples/printing/tables.rb +141 -0
- data/gemspec.yml +3 -3
- data/lib/command_kit/bug_report.rb +105 -0
- data/lib/command_kit/colors.rb +4 -4
- data/lib/command_kit/edit.rb +54 -0
- data/lib/command_kit/env.rb +1 -1
- data/lib/command_kit/options/option.rb +5 -1
- data/lib/command_kit/options/option_value.rb +2 -2
- data/lib/command_kit/options/parser.rb +1 -1
- data/lib/command_kit/options/quiet.rb +1 -1
- data/lib/command_kit/options/verbose.rb +2 -2
- data/lib/command_kit/options/version.rb +10 -0
- data/lib/command_kit/options.rb +1 -1
- data/lib/command_kit/os.rb +1 -1
- data/lib/command_kit/printing/fields.rb +56 -0
- data/lib/command_kit/printing/indent.rb +1 -1
- data/lib/command_kit/printing/lists.rb +91 -0
- data/lib/command_kit/printing/tables/border_style.rb +169 -0
- data/lib/command_kit/printing/tables/cell_builder.rb +93 -0
- data/lib/command_kit/printing/tables/row_builder.rb +111 -0
- data/lib/command_kit/printing/tables/style.rb +198 -0
- data/lib/command_kit/printing/tables/table_builder.rb +145 -0
- data/lib/command_kit/printing/tables/table_formatter.rb +254 -0
- data/lib/command_kit/printing/tables.rb +208 -0
- data/lib/command_kit/stdio.rb +5 -1
- data/lib/command_kit/version.rb +1 -1
- data/spec/bug_report_spec.rb +266 -0
- data/spec/colors_spec.rb +6 -0
- data/spec/command_name_spec.rb +1 -1
- data/spec/edit_spec.rb +72 -0
- data/spec/options/option_spec.rb +12 -2
- data/spec/options/quiet_spec.rb +51 -0
- data/spec/options/verbose_spec.rb +51 -0
- data/spec/options/version_spec.rb +146 -0
- data/spec/pager_spec.rb +1 -1
- data/spec/printing/fields_spec.rb +167 -0
- data/spec/printing/lists_spec.rb +99 -0
- data/spec/printing/tables/border_style.rb +43 -0
- data/spec/printing/tables/cell_builer_spec.rb +135 -0
- data/spec/printing/tables/row_builder_spec.rb +165 -0
- data/spec/printing/tables/style_spec.rb +377 -0
- data/spec/printing/tables/table_builder_spec.rb +252 -0
- data/spec/printing/tables/table_formatter_spec.rb +1180 -0
- data/spec/printing/tables_spec.rb +1069 -0
- metadata +33 -7
| @@ -0,0 +1,208 @@ | |
| 1 | 
            +
            # frozen_string_literal: true
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            require 'command_kit/printing/indent'
         | 
| 4 | 
            +
            require 'command_kit/printing/tables/table_builder'
         | 
| 5 | 
            +
            require 'command_kit/printing/tables/style'
         | 
| 6 | 
            +
            require 'command_kit/printing/tables/table_formatter'
         | 
| 7 | 
            +
             | 
| 8 | 
            +
            module CommandKit
         | 
| 9 | 
            +
              module Printing
         | 
| 10 | 
            +
                #
         | 
| 11 | 
            +
                # Methods for printing tables.
         | 
| 12 | 
            +
                #
         | 
| 13 | 
            +
                # ## Examples
         | 
| 14 | 
            +
                #
         | 
| 15 | 
            +
                #     include CommandKit::Printing::Tables
         | 
| 16 | 
            +
                #
         | 
| 17 | 
            +
                #     def run
         | 
| 18 | 
            +
                #       header = ['A', 'B', 'C']
         | 
| 19 | 
            +
                #       table = [
         | 
| 20 | 
            +
                #         ['AAAA', 'BBBB', 'CCCC'],
         | 
| 21 | 
            +
                #         ['AAAA', 'BBBB', 'CCCC'],
         | 
| 22 | 
            +
                #         ['AAAA', 'BBBB', 'CCCC']
         | 
| 23 | 
            +
                #       ]
         | 
| 24 | 
            +
                #
         | 
| 25 | 
            +
                #       print_table table
         | 
| 26 | 
            +
                #       # AAAA  BBBB  CCCC 
         | 
| 27 | 
            +
                #       # AAAA  BBBB  CCCC 
         | 
| 28 | 
            +
                #       # AAAA  BBBB  CCCC 
         | 
| 29 | 
            +
                #
         | 
| 30 | 
            +
                #       print_table table, header: header
         | 
| 31 | 
            +
                #       #  A     B     C   
         | 
| 32 | 
            +
                #       #
         | 
| 33 | 
            +
                #       # AAAA  BBBB  CCCC 
         | 
| 34 | 
            +
                #       # AAAA  BBBB  CCCC 
         | 
| 35 | 
            +
                #       # AAAA  BBBB  CCCC 
         | 
| 36 | 
            +
                #
         | 
| 37 | 
            +
                #       print_table table, header: header,
         | 
| 38 | 
            +
                #                          border: :ascii
         | 
| 39 | 
            +
                #       # +------+------+------+
         | 
| 40 | 
            +
                #       # |  A   |  B   |  C   |
         | 
| 41 | 
            +
                #       # +------+------+------+
         | 
| 42 | 
            +
                #       # | AAAA | BBBB | CCCC |
         | 
| 43 | 
            +
                #       # | AAAA | BBBB | CCCC |
         | 
| 44 | 
            +
                #       # | AAAA | BBBB | CCCC |
         | 
| 45 | 
            +
                #       # +------+------+------+
         | 
| 46 | 
            +
                #
         | 
| 47 | 
            +
                #       print_table table, header: header,
         | 
| 48 | 
            +
                #                          border: :ascii,
         | 
| 49 | 
            +
                #                          separate_rows: true
         | 
| 50 | 
            +
                #       # +------+------+------+
         | 
| 51 | 
            +
                #       # |  A   |  B   |  C   |
         | 
| 52 | 
            +
                #       # +------+------+------+
         | 
| 53 | 
            +
                #       # | AAAA | BBBB | CCCC |
         | 
| 54 | 
            +
                #       # +------+------+------+
         | 
| 55 | 
            +
                #       # | AAAA | BBBB | CCCC |
         | 
| 56 | 
            +
                #       # +------+------+------+
         | 
| 57 | 
            +
                #       # | AAAA | BBBB | CCCC |
         | 
| 58 | 
            +
                #       # +------+------+------+
         | 
| 59 | 
            +
                #
         | 
| 60 | 
            +
                #       print_table table, header: header,
         | 
| 61 | 
            +
                #                          border: :line
         | 
| 62 | 
            +
                #       # ┌──────┬──────┬──────┐
         | 
| 63 | 
            +
                #       # │  A   │  B   │  C   │
         | 
| 64 | 
            +
                #       # ├──────┼──────┼──────┤
         | 
| 65 | 
            +
                #       # │ AAAA │ BBBB │ CCCC │
         | 
| 66 | 
            +
                #       # │ AAAA │ BBBB │ CCCC │
         | 
| 67 | 
            +
                #       # │ AAAA │ BBBB │ CCCC │
         | 
| 68 | 
            +
                #       # └──────┴──────┴──────┘
         | 
| 69 | 
            +
                #
         | 
| 70 | 
            +
                #       print_table table, header: header,
         | 
| 71 | 
            +
                #                          border: :double_line
         | 
| 72 | 
            +
                #       # ╔══════╦══════╦══════╗
         | 
| 73 | 
            +
                #       # ║  A   ║  B   ║  C   ║
         | 
| 74 | 
            +
                #       # ╠══════╬══════╬══════╣
         | 
| 75 | 
            +
                #       # ║ AAAA ║ BBBB ║ CCCC ║
         | 
| 76 | 
            +
                #       # ║ AAAA ║ BBBB ║ CCCC ║
         | 
| 77 | 
            +
                #       # ║ AAAA ║ BBBB ║ CCCC ║
         | 
| 78 | 
            +
                #       # ╚══════╩══════╩══════╝
         | 
| 79 | 
            +
                #
         | 
| 80 | 
            +
                #       uneven_table = [
         | 
| 81 | 
            +
                #         ['AAAAAA', 'B',       'CCCCCCC'],
         | 
| 82 | 
            +
                #         ['AAA',    'BBBB',    'CCC'    ],
         | 
| 83 | 
            +
                #         ['A',      'BBBBBBB', 'C'      ]
         | 
| 84 | 
            +
                #       ]
         | 
| 85 | 
            +
                #
         | 
| 86 | 
            +
                #       print_table uneven_table, header: header,
         | 
| 87 | 
            +
                #                                 justify: :left,
         | 
| 88 | 
            +
                #                                 justify_header: :left,
         | 
| 89 | 
            +
                #                                 border: :line
         | 
| 90 | 
            +
                #       # ┌────────┬─────────┬─────────┐
         | 
| 91 | 
            +
                #       # │ A      │ B       │ C       │
         | 
| 92 | 
            +
                #       # ├────────┼─────────┼─────────┤
         | 
| 93 | 
            +
                #       # │ AAAAAA │ B       │ CCCCCCC │
         | 
| 94 | 
            +
                #       # │ AAA    │ BBBB    │ CCC     │
         | 
| 95 | 
            +
                #       # │ A      │ BBBBBBB │ C       │
         | 
| 96 | 
            +
                #       # └────────┴─────────┴─────────┘
         | 
| 97 | 
            +
                #
         | 
| 98 | 
            +
                #       print_table uneven_table, header: header,
         | 
| 99 | 
            +
                #                                 justify: :right,
         | 
| 100 | 
            +
                #                                 justify_header: :right,
         | 
| 101 | 
            +
                #                                 border: :line
         | 
| 102 | 
            +
                #       # ┌────────┬─────────┬─────────┐
         | 
| 103 | 
            +
                #       # │      A │       B │       C │
         | 
| 104 | 
            +
                #       # ├────────┼─────────┼─────────┤
         | 
| 105 | 
            +
                #       # │ AAAAAA │       B │ CCCCCCC │
         | 
| 106 | 
            +
                #       # │    AAA │    BBBB │     CCC │
         | 
| 107 | 
            +
                #       # │      A │ BBBBBBB │       C │
         | 
| 108 | 
            +
                #       # └────────┴─────────┴─────────┘
         | 
| 109 | 
            +
                #
         | 
| 110 | 
            +
                #       print_table uneven_table, header: header,
         | 
| 111 | 
            +
                #                                 justify: :center,
         | 
| 112 | 
            +
                #                                 justify_header: :center,
         | 
| 113 | 
            +
                #                                 border: :line
         | 
| 114 | 
            +
                #       # ┌────────┬─────────┬─────────┐
         | 
| 115 | 
            +
                #       # │   A    │    B    │    C    │
         | 
| 116 | 
            +
                #       # ├────────┼─────────┼─────────┤
         | 
| 117 | 
            +
                #       # │ AAAAAA │    B    │ CCCCCCC │
         | 
| 118 | 
            +
                #       # │  AAA   │  BBBB   │   CCC   │
         | 
| 119 | 
            +
                #       # │   A    │ BBBBBBB │    C    │
         | 
| 120 | 
            +
                #       # └────────┴─────────┴─────────┘
         | 
| 121 | 
            +
                #
         | 
| 122 | 
            +
                #       table_with_empty_cells = [
         | 
| 123 | 
            +
                #         ['AAAA', 'BBBB', 'CCCC'],
         | 
| 124 | 
            +
                #         ['AAAA', nil,    'CCCC'],
         | 
| 125 | 
            +
                #         ['AAAA', 'BBBB']
         | 
| 126 | 
            +
                #       ]
         | 
| 127 | 
            +
                #
         | 
| 128 | 
            +
                #       print_table table_with_empty_cells, header:  header,
         | 
| 129 | 
            +
                #                                           justify: :left,
         | 
| 130 | 
            +
                #                                           border:  :line
         | 
| 131 | 
            +
                #       # ┌──────┬──────┬──────┐
         | 
| 132 | 
            +
                #       # │  A   │  B   │  C   │
         | 
| 133 | 
            +
                #       # ├──────┼──────┼──────┤
         | 
| 134 | 
            +
                #       # │ AAAA │ BBBB │ CCCC │
         | 
| 135 | 
            +
                #       # │ AAAA │      │ CCCC │
         | 
| 136 | 
            +
                #       # │ AAAA │ BBBB │      │
         | 
| 137 | 
            +
                #       # └──────┴──────┴──────┘
         | 
| 138 | 
            +
                #
         | 
| 139 | 
            +
                #       multi_line_table = [
         | 
| 140 | 
            +
                #         ['AAAA',        'BBBB',    "CCCC\nCC"],
         | 
| 141 | 
            +
                #         ['AAAA',        "BBBB\nB", 'CCCC'],
         | 
| 142 | 
            +
                #         ["AAAA\nAA\nA", "BBBB",    "CCCC"]
         | 
| 143 | 
            +
                #       ]
         | 
| 144 | 
            +
                #
         | 
| 145 | 
            +
                #       print_table multi_line_table, header:  header,
         | 
| 146 | 
            +
                #                                     justify: :left,
         | 
| 147 | 
            +
                #                                     border:  :line
         | 
| 148 | 
            +
                #       # ┌──────┬──────┬──────┐
         | 
| 149 | 
            +
                #       # │  A   │  B   │  C   │
         | 
| 150 | 
            +
                #       # ├──────┼──────┼──────┤
         | 
| 151 | 
            +
                #       # │ AAAA │ BBBB │ CCCC │
         | 
| 152 | 
            +
                #       # │      │      │ CC   │
         | 
| 153 | 
            +
                #       # │ AAAA │ BBBB │ CCCC │
         | 
| 154 | 
            +
                #       # │      │ B    │      │
         | 
| 155 | 
            +
                #       # │ AAAA │ BBBB │ CCCC │
         | 
| 156 | 
            +
                #       # │ AA   │      │      │
         | 
| 157 | 
            +
                #       # │ A    │      │      │
         | 
| 158 | 
            +
                #       # └──────┴──────┴──────┘
         | 
| 159 | 
            +
                #     end
         | 
| 160 | 
            +
                #
         | 
| 161 | 
            +
                # @since 0.4.0
         | 
| 162 | 
            +
                #
         | 
| 163 | 
            +
                module Tables
         | 
| 164 | 
            +
                  include Indent
         | 
| 165 | 
            +
             | 
| 166 | 
            +
                  #
         | 
| 167 | 
            +
                  # Prints a table of rows.
         | 
| 168 | 
            +
                  #
         | 
| 169 | 
            +
                  # @param [Array<Array>] rows
         | 
| 170 | 
            +
                  #   The table rows.
         | 
| 171 | 
            +
                  #
         | 
| 172 | 
            +
                  # @param [Array, nil] header
         | 
| 173 | 
            +
                  #   The optional header row.
         | 
| 174 | 
            +
                  #
         | 
| 175 | 
            +
                  # @param [Hash{Symbol => Object}] kwargs
         | 
| 176 | 
            +
                  #   Additional keyword arguments.
         | 
| 177 | 
            +
                  #
         | 
| 178 | 
            +
                  # @option kwargs [:line, :double_line, nil, Hash{Symbol => String}, :ascii] :border
         | 
| 179 | 
            +
                  #   The border style or a custom Hash of border characters.
         | 
| 180 | 
            +
                  #
         | 
| 181 | 
            +
                  # @option [Integer] :padding (1)
         | 
| 182 | 
            +
                  #   The number of characters to pad table cell values with.
         | 
| 183 | 
            +
                  #
         | 
| 184 | 
            +
                  # @option [:left, :right, :center] :justify (:left)
         | 
| 185 | 
            +
                  #   Specifies how to justify the table cell values.
         | 
| 186 | 
            +
                  #
         | 
| 187 | 
            +
                  # @option [:left, :right, :center] :justify_header (:center)
         | 
| 188 | 
            +
                  #   Specifies how to justify the table header cell values.
         | 
| 189 | 
            +
                  #
         | 
| 190 | 
            +
                  # @option [Boolean] :separate_rows (false)
         | 
| 191 | 
            +
                  #   Specifies whether to add separator rows in between the rows.
         | 
| 192 | 
            +
                  #
         | 
| 193 | 
            +
                  # @api public
         | 
| 194 | 
            +
                  #
         | 
| 195 | 
            +
                  def print_table(rows, header: nil, **kwargs)
         | 
| 196 | 
            +
                    table     = TableBuilder.new(rows, header: header)
         | 
| 197 | 
            +
                    style     = Style.new(**kwargs)
         | 
| 198 | 
            +
                    formatter = TableFormatter.new(table,style)
         | 
| 199 | 
            +
             | 
| 200 | 
            +
                    formatter.format do |line|
         | 
| 201 | 
            +
                      puts line
         | 
| 202 | 
            +
                    end
         | 
| 203 | 
            +
             | 
| 204 | 
            +
                    return nil
         | 
| 205 | 
            +
                  end
         | 
| 206 | 
            +
                end
         | 
| 207 | 
            +
              end
         | 
| 208 | 
            +
            end
         | 
    
        data/lib/command_kit/stdio.rb
    CHANGED
    
    
    
        data/lib/command_kit/version.rb
    CHANGED
    
    
| @@ -0,0 +1,266 @@ | |
| 1 | 
            +
            require 'spec_helper'
         | 
| 2 | 
            +
            require 'command_kit/bug_report'
         | 
| 3 | 
            +
             | 
| 4 | 
            +
            describe CommandKit::BugReport do
         | 
| 5 | 
            +
              module TestBugReport
         | 
| 6 | 
            +
                class CommandWithoutBugReportURLSet
         | 
| 7 | 
            +
                  include CommandKit::BugReport
         | 
| 8 | 
            +
                end
         | 
| 9 | 
            +
             | 
| 10 | 
            +
                class CommandWithBugReportURLSet
         | 
| 11 | 
            +
                  include CommandKit::BugReport
         | 
| 12 | 
            +
             | 
| 13 | 
            +
                  bug_report_url 'https://github.com/org/repo/issues/new'
         | 
| 14 | 
            +
                end
         | 
| 15 | 
            +
             | 
| 16 | 
            +
                class CommandWithInheritedBugReportURL < CommandWithBugReportURLSet
         | 
| 17 | 
            +
                end
         | 
| 18 | 
            +
              end
         | 
| 19 | 
            +
             | 
| 20 | 
            +
              let(:command_class) { TestBugReport::CommandWithBugReportURLSet }
         | 
| 21 | 
            +
             | 
| 22 | 
            +
              describe ".included" do
         | 
| 23 | 
            +
                it { expect(command_class).to include(CommandKit::ExceptionHandler) }
         | 
| 24 | 
            +
                it { expect(command_class).to include(CommandKit::Printing) }
         | 
| 25 | 
            +
              end
         | 
| 26 | 
            +
             | 
| 27 | 
            +
              describe ".bug_report_url" do
         | 
| 28 | 
            +
                subject { TestBugReport::CommandWithoutBugReportURLSet }
         | 
| 29 | 
            +
             | 
| 30 | 
            +
                context "when no bug_report_url has been set" do
         | 
| 31 | 
            +
                  it "should default to nil" do
         | 
| 32 | 
            +
                    expect(subject.bug_report_url).to be_nil
         | 
| 33 | 
            +
                  end
         | 
| 34 | 
            +
                end
         | 
| 35 | 
            +
             | 
| 36 | 
            +
                context "when a bug_report_url is explicitly set" do
         | 
| 37 | 
            +
                  subject { TestBugReport::CommandWithBugReportURLSet }
         | 
| 38 | 
            +
             | 
| 39 | 
            +
                  it "must return the explicitly set bug_report_url" do
         | 
| 40 | 
            +
                    expect(subject.bug_report_url).to eq("https://github.com/org/repo/issues/new")
         | 
| 41 | 
            +
                  end
         | 
| 42 | 
            +
                end
         | 
| 43 | 
            +
             | 
| 44 | 
            +
                context "when the command class inherites from another class" do
         | 
| 45 | 
            +
                  context "but no bug_report_url is set" do
         | 
| 46 | 
            +
                    module TestBugReport
         | 
| 47 | 
            +
                      class BaseCmd
         | 
| 48 | 
            +
                        include CommandKit::BugReport
         | 
| 49 | 
            +
                      end
         | 
| 50 | 
            +
             | 
| 51 | 
            +
                      class InheritedCmd < BaseCmd
         | 
| 52 | 
            +
                      end
         | 
| 53 | 
            +
                    end
         | 
| 54 | 
            +
             | 
| 55 | 
            +
                    subject { TestBugReport::InheritedCmd }
         | 
| 56 | 
            +
             | 
| 57 | 
            +
                    it "must search each class then return nil "do
         | 
| 58 | 
            +
                      expect(subject.bug_report_url).to be_nil
         | 
| 59 | 
            +
                    end
         | 
| 60 | 
            +
                  end
         | 
| 61 | 
            +
             | 
| 62 | 
            +
                  module TestBugReport
         | 
| 63 | 
            +
                    class ExplicitBaseCmd
         | 
| 64 | 
            +
                      include CommandKit::BugReport
         | 
| 65 | 
            +
                      bug_report_url 'https://github.com/org/repo/issues/new'
         | 
| 66 | 
            +
                    end
         | 
| 67 | 
            +
                  end
         | 
| 68 | 
            +
             | 
| 69 | 
            +
                  context "when the superclass defines an explicit bug_report_url" do
         | 
| 70 | 
            +
                    module TestBugReport
         | 
| 71 | 
            +
                      class ImplicitInheritedCmd < ExplicitBaseCmd
         | 
| 72 | 
            +
                      end
         | 
| 73 | 
            +
                    end
         | 
| 74 | 
            +
             | 
| 75 | 
            +
                    let(:super_subject) { TestBugReport::ExplicitBaseCmd }
         | 
| 76 | 
            +
                    subject { TestBugReport::ImplicitInheritedCmd }
         | 
| 77 | 
            +
             | 
| 78 | 
            +
                    it "must inherit the superclass'es bug_report_url" do
         | 
| 79 | 
            +
                      expect(subject.bug_report_url).to eq(super_subject.bug_report_url)
         | 
| 80 | 
            +
                    end
         | 
| 81 | 
            +
             | 
| 82 | 
            +
                    it "must not change the superclass'es bug_report_url" do
         | 
| 83 | 
            +
                      expect(super_subject.bug_report_url).to eq('https://github.com/org/repo/issues/new')
         | 
| 84 | 
            +
                    end
         | 
| 85 | 
            +
                  end
         | 
| 86 | 
            +
             | 
| 87 | 
            +
                  context "when the subclass defines an explicit bug_report_url" do
         | 
| 88 | 
            +
                    module TestBugReport
         | 
| 89 | 
            +
                      class ImplicitBaseCmd
         | 
| 90 | 
            +
                        include CommandKit::BugReport
         | 
| 91 | 
            +
                      end
         | 
| 92 | 
            +
             | 
| 93 | 
            +
                      class ExplicitInheritedCmd < ImplicitBaseCmd
         | 
| 94 | 
            +
                        bug_report_url 'https://github.com/other_org/other_repo/issues/new'
         | 
| 95 | 
            +
                      end
         | 
| 96 | 
            +
                    end
         | 
| 97 | 
            +
             | 
| 98 | 
            +
                    let(:super_subject) { TestBugReport::ImplicitBaseCmd }
         | 
| 99 | 
            +
                    subject { TestBugReport::ExplicitInheritedCmd }
         | 
| 100 | 
            +
             | 
| 101 | 
            +
                    it "must return the subclass'es bug_report_url" do
         | 
| 102 | 
            +
                      expect(subject.bug_report_url).to eq('https://github.com/other_org/other_repo/issues/new')
         | 
| 103 | 
            +
                    end
         | 
| 104 | 
            +
             | 
| 105 | 
            +
                    it "must not change the superclass'es bug_report_url" do
         | 
| 106 | 
            +
                      expect(super_subject.bug_report_url).to be_nil
         | 
| 107 | 
            +
                    end
         | 
| 108 | 
            +
                  end
         | 
| 109 | 
            +
             | 
| 110 | 
            +
                  context "when both the subclass overrides the superclass's bug_report_urls" do
         | 
| 111 | 
            +
                    module TestBugReport
         | 
| 112 | 
            +
                      class ExplicitOverridingInheritedCmd < ExplicitBaseCmd
         | 
| 113 | 
            +
                        bug_report_url 'https://github.com/other_org/other_repo/issues/new'
         | 
| 114 | 
            +
                      end
         | 
| 115 | 
            +
                    end
         | 
| 116 | 
            +
             | 
| 117 | 
            +
                    let(:super_subject) { TestBugReport::ExplicitBaseCmd }
         | 
| 118 | 
            +
                    subject { TestBugReport::ExplicitOverridingInheritedCmd }
         | 
| 119 | 
            +
             | 
| 120 | 
            +
                    it "must return the subclass'es bug_report_url" do
         | 
| 121 | 
            +
                      expect(subject.bug_report_url).to eq("https://github.com/other_org/other_repo/issues/new")
         | 
| 122 | 
            +
                    end
         | 
| 123 | 
            +
             | 
| 124 | 
            +
                    it "must not change the superclass'es bug_report_url" do
         | 
| 125 | 
            +
                      expect(super_subject.bug_report_url).to eq("https://github.com/org/repo/issues/new")
         | 
| 126 | 
            +
                    end
         | 
| 127 | 
            +
                  end
         | 
| 128 | 
            +
                end
         | 
| 129 | 
            +
              end
         | 
| 130 | 
            +
             | 
| 131 | 
            +
              subject { command_class.new }
         | 
| 132 | 
            +
             | 
| 133 | 
            +
              describe "#bug_report_url" do
         | 
| 134 | 
            +
                context "when the command has bug_report_url set" do
         | 
| 135 | 
            +
                  let(:command_class) { TestBugReport::CommandWithBugReportURLSet }
         | 
| 136 | 
            +
             | 
| 137 | 
            +
                  it "must return the bug_report_url" do
         | 
| 138 | 
            +
                    expect(subject.bug_report_url).to eq(command_class.bug_report_url)
         | 
| 139 | 
            +
                  end
         | 
| 140 | 
            +
                end
         | 
| 141 | 
            +
             | 
| 142 | 
            +
                context "when the command does not have bug_report_url set" do
         | 
| 143 | 
            +
                  let(:command_class) { TestBugReport::CommandWithoutBugReportURLSet }
         | 
| 144 | 
            +
             | 
| 145 | 
            +
                  it "must return nil" do
         | 
| 146 | 
            +
                    expect(subject.bug_report_url).to be(nil)
         | 
| 147 | 
            +
                  end
         | 
| 148 | 
            +
                end
         | 
| 149 | 
            +
              end
         | 
| 150 | 
            +
             | 
| 151 | 
            +
              describe "#print_bug_report" do
         | 
| 152 | 
            +
                let(:message) { "error!" }
         | 
| 153 | 
            +
                let(:backtrace) do
         | 
| 154 | 
            +
                  [
         | 
| 155 | 
            +
                    "/path/to/test1.rb:1 in `test1'",
         | 
| 156 | 
            +
                    "/path/to/test2.rb:2 in `test2'",
         | 
| 157 | 
            +
                    "/path/to/test3.rb:3 in `test3'",
         | 
| 158 | 
            +
                    "/path/to/test4.rb:4 in `test4'"
         | 
| 159 | 
            +
                  ]
         | 
| 160 | 
            +
                end
         | 
| 161 | 
            +
                let(:exception) do
         | 
| 162 | 
            +
                  error = RuntimeError.new(message)
         | 
| 163 | 
            +
                  error.set_backtrace(backtrace)
         | 
| 164 | 
            +
                  error
         | 
| 165 | 
            +
                end
         | 
| 166 | 
            +
             | 
| 167 | 
            +
                subject { command_class.new(stderr: StringIO.new) }
         | 
| 168 | 
            +
             | 
| 169 | 
            +
                context "when the command has bug_report_url set" do
         | 
| 170 | 
            +
                  let(:command_class) { TestBugReport::CommandWithBugReportURLSet }
         | 
| 171 | 
            +
             | 
| 172 | 
            +
                  context "when stderr is a TTY" do
         | 
| 173 | 
            +
                    before { expect(subject.stderr).to receive(:tty?).and_return(true) }
         | 
| 174 | 
            +
             | 
| 175 | 
            +
                    it "must print a message, bug_report_url, and a highlighted exception" do
         | 
| 176 | 
            +
                      subject.print_bug_report(exception)
         | 
| 177 | 
            +
             | 
| 178 | 
            +
                      expect(subject.stderr.string).to eq(
         | 
| 179 | 
            +
                        [
         | 
| 180 | 
            +
                          '',
         | 
| 181 | 
            +
                          'Oops! Looks like you have found a bug. Please report it!',
         | 
| 182 | 
            +
                          command_class.bug_report_url,
         | 
| 183 | 
            +
                          '',
         | 
| 184 | 
            +
                          '```',
         | 
| 185 | 
            +
                          exception.full_message(highlight: true).chomp,
         | 
| 186 | 
            +
                          '```',
         | 
| 187 | 
            +
                          ''
         | 
| 188 | 
            +
                        ].join($/)
         | 
| 189 | 
            +
                      )
         | 
| 190 | 
            +
                    end
         | 
| 191 | 
            +
                  end
         | 
| 192 | 
            +
             | 
| 193 | 
            +
                  context "when stderr is not a TTY" do
         | 
| 194 | 
            +
                    it "must print a message, bug_report_url, and print an unhighlighted exception" do
         | 
| 195 | 
            +
                      subject.print_bug_report(exception)
         | 
| 196 | 
            +
             | 
| 197 | 
            +
                      expect(subject.stderr.string).to eq(
         | 
| 198 | 
            +
                        [
         | 
| 199 | 
            +
                          '',
         | 
| 200 | 
            +
                          'Oops! Looks like you have found a bug. Please report it!',
         | 
| 201 | 
            +
                          command_class.bug_report_url,
         | 
| 202 | 
            +
                          '',
         | 
| 203 | 
            +
                          '```',
         | 
| 204 | 
            +
                          exception.full_message(highlight: false).chomp,
         | 
| 205 | 
            +
                          '```',
         | 
| 206 | 
            +
                          ''
         | 
| 207 | 
            +
                        ].join($/)
         | 
| 208 | 
            +
                      )
         | 
| 209 | 
            +
                    end
         | 
| 210 | 
            +
                  end
         | 
| 211 | 
            +
                end
         | 
| 212 | 
            +
             | 
| 213 | 
            +
                context "when the command does not have bug_report_url set" do
         | 
| 214 | 
            +
                  let(:command_class) { TestBugReport::CommandWithoutBugReportURLSet }
         | 
| 215 | 
            +
             | 
| 216 | 
            +
                  context "when stderr is a TTY" do
         | 
| 217 | 
            +
                    before { expect(subject.stderr).to receive(:tty?).and_return(true) }
         | 
| 218 | 
            +
             | 
| 219 | 
            +
                    it "must print a message and a highlighted exception" do
         | 
| 220 | 
            +
                      subject.print_bug_report(exception)
         | 
| 221 | 
            +
             | 
| 222 | 
            +
                      expect(subject.stderr.string).to eq(
         | 
| 223 | 
            +
                        [
         | 
| 224 | 
            +
                          '',
         | 
| 225 | 
            +
                          'Oops! Looks like you have found a bug. Please report it!',
         | 
| 226 | 
            +
                          '',
         | 
| 227 | 
            +
                          '```',
         | 
| 228 | 
            +
                          exception.full_message(highlight: true).chomp,
         | 
| 229 | 
            +
                          '```',
         | 
| 230 | 
            +
                          ''
         | 
| 231 | 
            +
                        ].join($/)
         | 
| 232 | 
            +
                      )
         | 
| 233 | 
            +
                    end
         | 
| 234 | 
            +
                  end
         | 
| 235 | 
            +
             | 
| 236 | 
            +
                  context "when stderr is not a TTY" do
         | 
| 237 | 
            +
                    it "must print a message and print an unhighlighted exception" do
         | 
| 238 | 
            +
                      subject.print_bug_report(exception)
         | 
| 239 | 
            +
             | 
| 240 | 
            +
                      expect(subject.stderr.string).to eq(
         | 
| 241 | 
            +
                        [
         | 
| 242 | 
            +
                          '',
         | 
| 243 | 
            +
                          'Oops! Looks like you have found a bug. Please report it!',
         | 
| 244 | 
            +
                          '',
         | 
| 245 | 
            +
                          '```',
         | 
| 246 | 
            +
                          exception.full_message(highlight: false).chomp,
         | 
| 247 | 
            +
                          '```',
         | 
| 248 | 
            +
                          ''
         | 
| 249 | 
            +
                        ].join($/)
         | 
| 250 | 
            +
                      )
         | 
| 251 | 
            +
                    end
         | 
| 252 | 
            +
                  end
         | 
| 253 | 
            +
                end
         | 
| 254 | 
            +
              end
         | 
| 255 | 
            +
             | 
| 256 | 
            +
              describe "#on_exception" do
         | 
| 257 | 
            +
                let(:exception) { RuntimeError.new('error!') }
         | 
| 258 | 
            +
             | 
| 259 | 
            +
                it "must call print_bug_report with the exception and then exit(-1)" do
         | 
| 260 | 
            +
                  expect(subject).to receive(:print_bug_report).with(exception)
         | 
| 261 | 
            +
                  expect(subject).to receive(:exit).with(-1)
         | 
| 262 | 
            +
             | 
| 263 | 
            +
                  subject.on_exception(exception)
         | 
| 264 | 
            +
                end
         | 
| 265 | 
            +
              end
         | 
| 266 | 
            +
            end
         | 
    
        data/spec/colors_spec.rb
    CHANGED
    
    | @@ -656,6 +656,12 @@ describe CommandKit::Colors do | |
| 656 656 | 
             
                  it { expect(subject.ansi?).to be(false) }
         | 
| 657 657 | 
             
                end
         | 
| 658 658 |  | 
| 659 | 
            +
                context "when NO_COLOR is set" do
         | 
| 660 | 
            +
                  subject { command_class.new(env: {'NO_COLOR' => 'true'}) }
         | 
| 661 | 
            +
             | 
| 662 | 
            +
                  it { expect(subject.ansi?).to be(false) }
         | 
| 663 | 
            +
                end
         | 
| 664 | 
            +
             | 
| 659 665 | 
             
                context "when stdout is a TTY" do
         | 
| 660 666 | 
             
                  let(:stdout) { StringIO.new }
         | 
| 661 667 | 
             
                  subject { command_class.new(stdout: stdout) }
         | 
    
        data/spec/command_name_spec.rb
    CHANGED
    
    
    
        data/spec/edit_spec.rb
    ADDED
    
    | @@ -0,0 +1,72 @@ | |
| 1 | 
            +
            require 'spec_helper'
         | 
| 2 | 
            +
            require 'command_kit/edit'
         | 
| 3 | 
            +
             | 
| 4 | 
            +
            describe CommandKit::Edit do
         | 
| 5 | 
            +
              module TestEdit
         | 
| 6 | 
            +
                class TestCommand
         | 
| 7 | 
            +
                  include CommandKit::Edit
         | 
| 8 | 
            +
                end
         | 
| 9 | 
            +
              end
         | 
| 10 | 
            +
             | 
| 11 | 
            +
              let(:command_class) { TestEdit::TestCommand }
         | 
| 12 | 
            +
             | 
| 13 | 
            +
              it "must also include CommandKit::Env" do
         | 
| 14 | 
            +
                expect(command_class).to include(CommandKit::Env)
         | 
| 15 | 
            +
              end
         | 
| 16 | 
            +
             | 
| 17 | 
            +
              describe "#editor" do
         | 
| 18 | 
            +
                subject { command_class.new(env: env) }
         | 
| 19 | 
            +
             | 
| 20 | 
            +
                context "when env['EDITOR'] is set" do
         | 
| 21 | 
            +
                  let(:editor) { 'vim' }
         | 
| 22 | 
            +
                  let(:env) do
         | 
| 23 | 
            +
                    {'EDITOR' => editor}
         | 
| 24 | 
            +
                  end
         | 
| 25 | 
            +
             | 
| 26 | 
            +
                  it "must return env['EDITOR']" do
         | 
| 27 | 
            +
                    expect(subject.editor).to eq(env['EDITOR'])
         | 
| 28 | 
            +
                  end
         | 
| 29 | 
            +
                end
         | 
| 30 | 
            +
             | 
| 31 | 
            +
                context "when env['EDITOR'] is not set" do
         | 
| 32 | 
            +
                  let(:env) do
         | 
| 33 | 
            +
                    {}
         | 
| 34 | 
            +
                  end
         | 
| 35 | 
            +
             | 
| 36 | 
            +
                  it "must return 'nano'" do
         | 
| 37 | 
            +
                    expect(subject.editor).to eq('nano')
         | 
| 38 | 
            +
                  end
         | 
| 39 | 
            +
                end
         | 
| 40 | 
            +
              end
         | 
| 41 | 
            +
             | 
| 42 | 
            +
              describe "#edit" do
         | 
| 43 | 
            +
                subject { command_class.new(env: env) }
         | 
| 44 | 
            +
             | 
| 45 | 
            +
                let(:arguments) { ['file.txt'] }
         | 
| 46 | 
            +
             | 
| 47 | 
            +
                context "when env['EDITOR'] is set" do
         | 
| 48 | 
            +
                  let(:editor) { 'vim' }
         | 
| 49 | 
            +
                  let(:env) do
         | 
| 50 | 
            +
                    {'EDITOR' => editor}
         | 
| 51 | 
            +
                  end
         | 
| 52 | 
            +
             | 
| 53 | 
            +
                  it "must invoke system with #editor and the additional arguments" do
         | 
| 54 | 
            +
                    expect(subject).to receive(:system).with(subject.editor,*arguments)
         | 
| 55 | 
            +
             | 
| 56 | 
            +
                    subject.edit(*arguments)
         | 
| 57 | 
            +
                  end
         | 
| 58 | 
            +
                end
         | 
| 59 | 
            +
             | 
| 60 | 
            +
                context "when env['EDITOR'] is not set" do
         | 
| 61 | 
            +
                  let(:env) do
         | 
| 62 | 
            +
                    {}
         | 
| 63 | 
            +
                  end
         | 
| 64 | 
            +
             | 
| 65 | 
            +
                  it "must invoke system with 'nano' and the additional arguments" do
         | 
| 66 | 
            +
                    expect(subject).to receive(:system).with('nano',*arguments)
         | 
| 67 | 
            +
             | 
| 68 | 
            +
                    subject.edit(*arguments)
         | 
| 69 | 
            +
                  end
         | 
| 70 | 
            +
                end
         | 
| 71 | 
            +
              end
         | 
| 72 | 
            +
            end
         | 
    
        data/spec/options/option_spec.rb
    CHANGED
    
    | @@ -6,10 +6,12 @@ describe CommandKit::Options::Option do | |
| 6 6 | 
             
              let(:short)  { nil     }
         | 
| 7 7 | 
             
              let(:long)   { '--foo' }
         | 
| 8 8 | 
             
              let(:equals) { false   }
         | 
| 9 | 
            +
              let(:value_usage)    { 'FOO' }
         | 
| 10 | 
            +
              let(:value_required) { true  }
         | 
| 9 11 | 
             
              let(:value) do
         | 
| 10 12 | 
             
                {
         | 
| 11 | 
            -
                  usage:     | 
| 12 | 
            -
                  required:  | 
| 13 | 
            +
                  usage:    value_usage,
         | 
| 14 | 
            +
                  required: value_required
         | 
| 13 15 | 
             
                }
         | 
| 14 16 | 
             
              end
         | 
| 15 17 | 
             
              let(:desc)  { 'Foo option' }
         | 
| @@ -228,6 +230,14 @@ describe CommandKit::Options::Option do | |
| 228 230 | 
             
                    it "must return '--option=USAGE'" do
         | 
| 229 231 | 
             
                      expect(subject.usage.last).to eq("#{long}=#{subject.value.usage}")
         | 
| 230 232 | 
             
                    end
         | 
| 233 | 
            +
             | 
| 234 | 
            +
                    context "but the #value is also optional?" do
         | 
| 235 | 
            +
                      let(:value_required) { false }
         | 
| 236 | 
            +
             | 
| 237 | 
            +
                      it "must return '--option[=USAGE]'" do
         | 
| 238 | 
            +
                        expect(subject.usage.last).to eq("#{long}[=#{subject.value.usage}]")
         | 
| 239 | 
            +
                      end
         | 
| 240 | 
            +
                    end
         | 
| 231 241 | 
             
                  end
         | 
| 232 242 | 
             
                end
         | 
| 233 243 |  | 
| @@ -0,0 +1,51 @@ | |
| 1 | 
            +
            require 'spec_helper'
         | 
| 2 | 
            +
            require 'command_kit/options/quiet'
         | 
| 3 | 
            +
             | 
| 4 | 
            +
            describe CommandKit::Options::Quiet do
         | 
| 5 | 
            +
              module TestOptionsQuiet
         | 
| 6 | 
            +
                class TestCommand
         | 
| 7 | 
            +
                  include CommandKit::Options::Quiet
         | 
| 8 | 
            +
                end
         | 
| 9 | 
            +
              end
         | 
| 10 | 
            +
             | 
| 11 | 
            +
              let(:command_class) { TestOptionsQuiet::TestCommand }
         | 
| 12 | 
            +
             | 
| 13 | 
            +
              describe ".included" do
         | 
| 14 | 
            +
                subject { command_class }
         | 
| 15 | 
            +
             | 
| 16 | 
            +
                it "must include CommandKit::Options" do
         | 
| 17 | 
            +
                  expect(subject).to include(CommandKit::Options)
         | 
| 18 | 
            +
                end
         | 
| 19 | 
            +
             | 
| 20 | 
            +
                it "must define a quiet option" do
         | 
| 21 | 
            +
                  expect(subject.options[:quiet]).to_not be(nil)
         | 
| 22 | 
            +
                  expect(subject.options[:quiet].short).to eq('-q')
         | 
| 23 | 
            +
                  expect(subject.options[:quiet].long).to eq('--quiet')
         | 
| 24 | 
            +
                  expect(subject.options[:quiet].desc).to eq('Enables quiet output')
         | 
| 25 | 
            +
                end
         | 
| 26 | 
            +
              end
         | 
| 27 | 
            +
             | 
| 28 | 
            +
              subject { command_class.new }
         | 
| 29 | 
            +
             | 
| 30 | 
            +
              describe "#quiet?" do
         | 
| 31 | 
            +
                context "when @quiet is true" do
         | 
| 32 | 
            +
                  before do
         | 
| 33 | 
            +
                    subject.instance_variable_set('@quiet',true)
         | 
| 34 | 
            +
                  end
         | 
| 35 | 
            +
             | 
| 36 | 
            +
                  it "must return true" do
         | 
| 37 | 
            +
                    expect(subject.quiet?).to be(true)
         | 
| 38 | 
            +
                  end
         | 
| 39 | 
            +
                end
         | 
| 40 | 
            +
             | 
| 41 | 
            +
                context "when @quiet is false" do
         | 
| 42 | 
            +
                  before do
         | 
| 43 | 
            +
                    subject.instance_variable_set('@quiet',false)
         | 
| 44 | 
            +
                  end
         | 
| 45 | 
            +
             | 
| 46 | 
            +
                  it "must return false" do
         | 
| 47 | 
            +
                    expect(subject.quiet?).to be(false)
         | 
| 48 | 
            +
                  end
         | 
| 49 | 
            +
                end
         | 
| 50 | 
            +
              end
         | 
| 51 | 
            +
            end
         |