From 3708b7c149570aeecceae8c83f5f98dca5e0fb36 Mon Sep 17 00:00:00 2001 From: Kevin Newton Date: Fri, 26 Aug 2022 11:56:22 -0400 Subject: [PATCH] More attempts to fix CI --- .github/workflows/gh-pages.yml | 6 ++++- lib/syntax_tree/cli.rb | 40 ++++++++++++++++++++++++++++------ test/cli_test.rb | 24 ++++++++++++++------ 3 files changed, 55 insertions(+), 15 deletions(-) diff --git a/.github/workflows/gh-pages.yml b/.github/workflows/gh-pages.yml index 8adc1b45..a8ce8d15 100644 --- a/.github/workflows/gh-pages.yml +++ b/.github/workflows/gh-pages.yml @@ -1,5 +1,9 @@ name: Github Pages (rdoc) -on: [push] +on: + push: + branches: + - main + jobs: build-and-deploy: runs-on: ubuntu-latest diff --git a/lib/syntax_tree/cli.rb b/lib/syntax_tree/cli.rb index 2de20e78..ca3647f5 100644 --- a/lib/syntax_tree/cli.rb +++ b/lib/syntax_tree/cli.rb @@ -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) @@ -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 @@ -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 @@ -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) @@ -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 diff --git a/test/cli_test.rb b/test/cli_test.rb index aec8f820..6743f759 100644 --- a/test/cli_test.rb +++ b/test/cli_test.rb @@ -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 @@ -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 @@ -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) @@ -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)