Skip to content

More attempts to fix CI #154

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
Aug 26, 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
6 changes: 5 additions & 1 deletion .github/workflows/gh-pages.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
name: Github Pages (rdoc)
on: [push]
on:
push:
branches:
- main

jobs:
build-and-deploy:
runs-on: ubuntu-latest
Expand Down
40 changes: 33 additions & 7 deletions lib/syntax_tree/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,15 @@ def handler
def source
handler.read(filepath)
end

def writable?
File.writable?(filepath)
end
end

# An item of work that corresponds to a script content passed via the
# command line.
class ScriptItem
FILEPATH = :script

attr_reader :source

def initialize(source)
Expand All @@ -69,7 +71,30 @@ def handler
end

def filepath
FILEPATH
:script
end

def writable?
false
end
end

# An item of work that correspond to the content passed in via stdin.
class STDINItem
def handler
HANDLERS[".rb"]
end

def filepath
:stdin
end

def source
$stdin.read
end

def writable?
false
end
end

Expand Down Expand Up @@ -196,7 +221,7 @@ def run(item)

source = item.source
formatted = item.handler.format(source, options.print_width)
File.write(filepath, formatted) if item.filepath != :script
File.write(filepath, formatted) if item.writable?

color = source == formatted ? Color.gray(filepath) : filepath
delta = ((Time.now - start) * 1000).round
Expand Down Expand Up @@ -386,7 +411,7 @@ def run(argv)
return 1
end

# If we're not reading from stdin and the user didn't supply and
# 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)
Expand All @@ -403,12 +428,13 @@ def run(argv)
Dir
.glob(pattern)
.each do |filepath|
queue << FileItem.new(filepath) if File.file?(filepath)
queue << FileItem.new(filepath) if File.readable?(filepath)
end
end

options.scripts.each { |script| queue << ScriptItem.new(script) }
else
queue << ScriptItem.new($stdin.read)
queue << STDINItem.new
end

# At the end, we're going to return whether or not this worker ever
Expand Down
24 changes: 17 additions & 7 deletions test/cli_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ def test_help_default
end

def test_no_arguments
$stdin.stub(:tty?, true) do
with_tty do
*, stderr = capture_io { SyntaxTree::CLI.run(["check"]) }
assert_includes(stderr, "stree help")
end
Expand All @@ -134,13 +134,17 @@ def test_no_arguments_no_tty
end

def test_inline_script
stdio, = capture_io { SyntaxTree::CLI.run(%w[format -e 1+1]) }
assert_equal("1 + 1\n", stdio)
with_tty do
stdio, = capture_io { SyntaxTree::CLI.run(%w[format -e 1+1]) }
assert_equal("1 + 1\n", stdio)
end
end

def test_multiple_inline_scripts
stdio, = capture_io { SyntaxTree::CLI.run(%w[format -e 1+1 -e 2+2]) }
assert_equal("1 + 1\n2 + 2\n", stdio)
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
end

def test_generic_error
Expand Down Expand Up @@ -241,8 +245,10 @@ def run_cli(command, *args, contents: :default)

status = nil
stdio, stderr =
capture_io do
status = SyntaxTree::CLI.run([command, *args, tempfile.path])
with_tty do
capture_io do
status = SyntaxTree::CLI.run([command, *args, tempfile.path])
end
end

Result.new(status: status, stdio: stdio, stderr: stderr)
Expand All @@ -251,6 +257,10 @@ 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