Skip to content

Fix CLI checking for content #161

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 28, 2022
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
13 changes: 3 additions & 10 deletions lib/syntax_tree/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
29 changes: 6 additions & 23 deletions test/cli_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down