Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 8 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,33 +24,27 @@ gem 'gem_dating', group: [:development], require: false

### Running GemDating

This gem provides a *very* limited command line interface. It may be invoked with:
This gem provides a small command line interface. It may be invoked with:

```bash
$ gem_dating [path/to/Gemfile]
$ gem_dating
```

Given a path to a Gemfile, GemDating will output a list of gems and their relative ages to the stdout stream.
By default, GemDating will look for a Gemfile in the current directory.
If it finds one, it will output a list of gems and their relative ages to the stdout stream.

For example(using gem exec):
```bash
$ gem exec gem_dating ~/code/my_app/Gemfile
```
You may also pass a path to a Gemfile as an argument:

or (using installed gem):
```bash
$ gem_dating ~/code/my_app/Gemfile
```

to see the output in your terminal.
```

Or you can run
GemDating leans on `$stdout`, so you can pipe the output to a file if you'd like:
```bash
$ gem_dating ~/code/my_app/Gemfile > ~/code/my_app/gem_ages.txt
```
which will pipe the output into a text file.

The command line output will look something like this:
[TablePrint](https://github.com/arches/table_print) formats the output to something like this:

```bash
NAME | VERSION | DATE
Expand Down
20 changes: 16 additions & 4 deletions lib/gem_dating/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,15 @@ class Cli

HELP_TEXT =
<<~HELP
Usage: gem_dating [--help | -h] [<GEMFILE_FILEPATH>]
gem_dating [--help | -h] [<GEMFILE_FILEPATH>]

GEMFILE_FILEPATH defaults to ./Gemfile if not provided.

Options:
--help, -h Show this help message
HELP

def initialize(argv)
def initialize(argv = [])
args, file_path = argv.partition { |arg| (arg =~ /--\w+/) || (arg =~ /(-[a-z])/) }

@args = args
Expand All @@ -22,8 +27,15 @@ def run
end

unless @file_path
$stdout << HELP_TEXT
return GENERAL_ERROR
current_directory = Dir.pwd
file_path = File.join(current_directory, "Gemfile")

if File.exist?(file_path)
@file_path = file_path
else
$stdout << HELP_TEXT
return GENERAL_ERROR
end
end

$stdout << GemDating.from_file(@file_path).table_print << "\n"
Expand Down
53 changes: 43 additions & 10 deletions test/cli_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,21 @@ def setup
s.version = "0.0.2227"
s.date = DateTime.parse("2023-05-17")
end
end

def test_gemfile
exit_code = nil
@tempdir = Dir.mktmpdir("gem_dating_test")

Mocktail.replace(GemDating::Rubygems)
stubs { |m| GemDating::Rubygems.fetch(m.is_a(Array)) }.with { [@spec1, @spec2, @spec3] }
end

def teardown
super
FileUtils.rm_rf @tempdir
Mocktail.reset
end

def test_gemfile
exit_code = nil

stdout, _stderr = capture_io do
exit_code = GemDating::Cli.new(["test/Gemfile.example"]).run
Expand All @@ -42,17 +50,42 @@ def test_gemfile
assert_equal expected_out, stdout
end

def test_no_args_prints_help
exit_code = nil
def test_default_to_existing_relative_gemfile
FileUtils.copy("test/Gemfile.example", "#{@tempdir}/Gemfile")

stdout, _stderr = capture_io do
exit_code = GemDating::Cli.new([]).run
Dir.chdir(@tempdir) do
exit_code = nil

stdout, _stderr = capture_io do
exit_code = GemDating::Cli.new.run
end

expected_out = <<~EXPECTED
NAME | VERSION | DATE
----------------|----------|-----------
banana-client | 21.1.0 | 1990-08-21
rails-on-rubies | 70.0.5 | 2123-05-24
giraffeql | 0.0.2227 | 2023-05-17
EXPECTED

assert_equal 0, exit_code
assert_equal expected_out, stdout
end
end

expected_out = GemDating::Cli::HELP_TEXT
def test_no_default_gemfile
Dir.chdir(@tempdir) do
exit_code = nil

assert_equal 1, exit_code
assert_equal expected_out, stdout
stdout, _stderr = capture_io do
exit_code = GemDating::Cli.new([]).run
end

expected_out = GemDating::Cli::HELP_TEXT

assert_equal 1, exit_code
assert_equal expected_out, stdout
end
end

def test_bad_filepath
Expand Down