From 1ea066bb3a6fa3864a287a48ccab26d194ed8242 Mon Sep 17 00:00:00 2001 From: Kevin Newton Date: Wed, 28 Sep 2022 14:52:53 -0400 Subject: [PATCH] Fix CLI checking for content Previously, we were checking if $stdin was a TTY to determine if there was content to be read. As it turns out, this isn't really a good indicator, as content could always come later, and some folks run stree in CI when $stdin is not a TTY and still pass filenames. Instead, we now check if no filenames were passed, and in that case we attempt to read from $stdin. --- lib/syntax_tree/cli.rb | 13 +++---------- test/cli_test.rb | 29 ++++++----------------------- 2 files changed, 9 insertions(+), 33 deletions(-) diff --git a/lib/syntax_tree/cli.rb b/lib/syntax_tree/cli.rb index a364cd34..f3564e29 100644 --- a/lib/syntax_tree/cli.rb +++ b/lib/syntax_tree/cli.rb @@ -423,19 +423,12 @@ def run(argv) return 1 end - # If we're not reading from stdin and the user didn't supply any - # filepaths to be read, then we exit with the usage message. - if $stdin.tty? && arguments.empty? && options.scripts.empty? - warn(HELP) - return 1 - end - # We're going to build up a queue of items to process. queue = Queue.new - # If we're reading from stdin, then we'll just add the stdin object to - # the queue. Otherwise, we'll add each of the filepaths to the queue. - if $stdin.tty? && (arguments.any? || options.scripts.any?) + # If there are any arguments or scripts, then we'll add those to the + # queue. Otherwise we'll read the content off STDIN. + if arguments.any? || options.scripts.any? arguments.each do |pattern| Dir .glob(pattern) diff --git a/test/cli_test.rb b/test/cli_test.rb index de09b093..3734e734 100644 --- a/test/cli_test.rb +++ b/test/cli_test.rb @@ -123,13 +123,6 @@ def test_help_default end def test_no_arguments - with_tty do - *, stderr = capture_io { SyntaxTree::CLI.run(["check"]) } - assert_includes(stderr, "stree help") - end - end - - def test_no_arguments_no_tty stdin = $stdin $stdin = StringIO.new("1+1") @@ -140,17 +133,13 @@ def test_no_arguments_no_tty end def test_inline_script - with_tty do - stdio, = capture_io { SyntaxTree::CLI.run(%w[format -e 1+1]) } - assert_equal("1 + 1\n", stdio) - end + stdio, = capture_io { SyntaxTree::CLI.run(%w[format -e 1+1]) } + assert_equal("1 + 1\n", stdio) end def test_multiple_inline_scripts - with_tty do - stdio, = capture_io { SyntaxTree::CLI.run(%w[format -e 1+1 -e 2+2]) } - assert_equal("1 + 1\n2 + 2\n", stdio) - end + stdio, = capture_io { SyntaxTree::CLI.run(%w[format -e 1+1 -e 2+2]) } + assert_equal("1 + 1\n2 + 2\n", stdio) end def test_generic_error @@ -251,10 +240,8 @@ def run_cli(command, *args, contents: :default) status = nil stdio, stderr = - with_tty do - capture_io do - status = SyntaxTree::CLI.run([command, *args, tempfile.path]) - end + capture_io do + status = SyntaxTree::CLI.run([command, *args, tempfile.path]) end Result.new(status: status, stdio: stdio, stderr: stderr) @@ -263,10 +250,6 @@ def run_cli(command, *args, contents: :default) tempfile.unlink end - def with_tty(&block) - $stdin.stub(:tty?, true, &block) - end - def with_config_file(contents) filepath = File.join(Dir.pwd, SyntaxTree::CLI::ConfigFile::FILENAME) File.write(filepath, contents)