Skip to content

Commit

Permalink
Merge pull request #61 from zombocom/schneems/get-that-cov
Browse files Browse the repository at this point in the history
Schneems/get that cov
  • Loading branch information
schneems authored Jul 15, 2024
2 parents 555dcfa + 83e479e commit 710e88c
Show file tree
Hide file tree
Showing 9 changed files with 169 additions and 46 deletions.
4 changes: 3 additions & 1 deletion bin/rundoc
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,7 @@ $: << File.expand_path(File.dirname(File.realpath(__FILE__)) + '/../lib')

require 'rundoc'

cli = Rundoc::CLIArgumentParser.new(argv: ARGV).to_cli
options = Rundoc::CLIArgumentParser.new(argv: ARGV).call.options

cli = Rundoc::CLI.new(**options)
cli.call
10 changes: 5 additions & 5 deletions lib/rundoc/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,20 @@ def initialize(
on_success_dir: nil,
on_failure_dir: nil,
output_filename: nil,
screenshots_dir: nil
screenshots_dirname: nil
)
@io = io
@force = force
@cli_cmd = cli_cmd
@cli_args = cli_args

screenshots_dir = check_relative_path(screenshots_dir || DEFAULTS::SCREENSHOTS_DIR)
screenshots_dirname = check_relative_path(screenshots_dirname || DEFAULTS::SCREENSHOTS_DIR)
output_filename = check_relative_path(output_filename || DEFAULTS::OUTPUT_FILENAME)

@execution_context = Rundoc::Context::Execution.new(
output_dir: Dir.mktmpdir,
source_path: source_path,
screenshots_dir: screenshots_dir
screenshots_dirname: screenshots_dirname
)

@after_build_context = Context::AfterBuild.new(
Expand All @@ -56,13 +56,13 @@ def initialize(
Pathname(on_success_dir)
else
@execution_context.source_dir.join(DEFAULTS::ON_SUCCESS_DIR)
end
end.expand_path

@on_failure_dir = if on_failure_dir
Pathname(on_failure_dir)
else
@execution_context.source_dir.join(DEFAULTS::ON_FAILURE_DIR)
end
end.expand_path
end

def force?
Expand Down
15 changes: 6 additions & 9 deletions lib/rundoc/cli_argument_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def initialize(
@exit_obj = exit_obj
end

def to_cli
def call
source_file = argv.first
if source_file.nil? || source_file == "help"
parser.parse! ["--help"]
Expand All @@ -37,24 +37,21 @@ def to_cli
return if options[:exit]
end

if source_file.nil?
@io.puts "No file given, run with `--help` for usage options."
exit_obj.exit(1)
end

source_path = Pathname(source_file)
if !source_path.exist?
@io.puts "No such file `#{source_path.expand_path}`"
exit_obj.exit(1)
return
elsif !source_path.file?
@io.puts "Expected `#{source_path.expand_path}` to be a file, but it was not."
@io.puts "Path is not a file. Expected `#{source_path.expand_path}` to be a file, but it was not."
exit_obj.exit(1)
return
end

options[:io] = io
options[:source_path] = source_path

CLI.new(**options)
self
end

private def parser
Expand Down Expand Up @@ -129,7 +126,7 @@ def to_cli
end

opts.on("--screenshots-dirname <name>", "Name of screenshot dir i.e. `#{CLI::DEFAULTS::SCREENSHOTS_DIR}`.") do |v|
options[:screenshots_dir] = v
options[:screenshots_dirname] = v
end

opts.on("--force", "Delete contents of the success/failure dirs even if they're not empty.") do |v|
Expand Down
17 changes: 0 additions & 17 deletions lib/rundoc/code_command/rundoc/depend_on.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,8 @@ class DependOn < ::Rundoc::CodeCommand
# Pass in the relative path of another rundoc document in order to
# run all of it's commands (but not to )
def initialize(path)
raise "Path must be relative (i.e. start with `.` or `..`. #{path.inspect} does not" unless path.start_with?(".")
@path = Pathname.new(path)
end

def to_md(env = {})
""
end

def call(env = {})
raise "rundoc.depend_on has been removed, use `:::-- rundoc.require` instead"
end

def hidden?
true
end

def not_hidden?
!hidden?
end
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/rundoc/context/execution.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ class Execution
# Directory to store screenshots, relative to output_dir
:screenshots_dir

def initialize(source_path:, output_dir:, screenshots_dir:)
def initialize(source_path:, output_dir:, screenshots_dirname:)
@source_path = Pathname(source_path).expand_path
@source_dir = @source_path.parent
@output_dir = Pathname(output_dir).expand_path
@screenshots_dir = @output_dir.join(screenshots_dir)
@screenshots_dir = @output_dir.join(screenshots_dirname).expand_path
end
end
end
Expand Down
9 changes: 7 additions & 2 deletions test/integration/website_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,15 @@ def test_screenshot_command
env = {}
env[:before] = []

screenshots_dir = Pathname(dir).join("screenshots")
screenshots_dirname = "screenshots"
screenshots_dir = Pathname(dir).join(screenshots_dirname)
screenshot_1_path = screenshots_dir.join("screenshot_1.png")

parsed = parse_contents(contents, screenshots_dir: screenshots_dir, output_dir: screenshots_dir.parent)
parsed = parse_contents(
contents,
output_dir: screenshots_dir.parent,
screenshots_dirname: screenshots_dirname
)
actual = parsed.to_md.gsub(Rundoc::CodeSection::AUTOGEN_WARNING, "")

expected = "![Screenshot of http://example.com/](screenshots/screenshot_1.png)"
Expand Down
118 changes: 118 additions & 0 deletions test/rundoc/cli_argument_parser_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
require "test_helper"

class CliArgumentParserTest < Minitest::Test
def test_help
io = StringIO.new
exit_obj = FakeExit.new
parser = Rundoc::CLIArgumentParser.new(
io: io,
argv: ["--help"],
env: {},
exit_obj: exit_obj
)
parser.call
output = io.string

assert output.include?("Prints this help output")
end

def test_help_no_flag
io = StringIO.new
exit_obj = FakeExit.new
parser = Rundoc::CLIArgumentParser.new(
io: io,
argv: ["help"],
env: {},
exit_obj: exit_obj
)
parser.call
output = io.string

assert output.include?("Prints this help output")
end

def test_no_such_file
io = StringIO.new
exit_obj = FakeExit.new
parser = Rundoc::CLIArgumentParser.new(
io: io,
argv: ["fileDoesNotExist.txt"],
env: {},
exit_obj: exit_obj
)
parser.call
output = io.string

assert exit_obj.value == 1
assert_includes output, "No such file"
end

def test_dir_not_file
Dir.mktmpdir do |dir|
io = StringIO.new
exit_obj = FakeExit.new
parser = Rundoc::CLIArgumentParser.new(
io: io,
argv: [dir],
env: {},
exit_obj: exit_obj
)
parser.call
output = io.string

assert exit_obj.value == 1
assert_includes output, "Path is not a file"
end
end

def test_valid_inputs
Dir.mktmpdir do |dir|
Dir.chdir(dir) do
tmp = Pathname(dir)
rundoc = tmp.join("rundoc.md")
rundoc.write("")

io = StringIO.new
exit_obj = FakeExit.new
parser = Rundoc::CLIArgumentParser.new(
io: io,
argv: [
rundoc,
"--on-success-dir=./success",
"--on-failure-dir=./failure",
"--dotenv-path=./lol/.env",
"--screenshots-dirname=pics",
"--output-filename=OUTPUT.md",
"--force"
],
env: {},
exit_obj: exit_obj
)
parser.call
output = io.string

assert !exit_obj.called?
assert output.empty?

assert_equal "./success", parser.options[:on_success_dir]
assert_equal "./failure", parser.options[:on_failure_dir]
assert_equal "./lol/.env", parser.options[:dotenv_path]
assert_equal "pics", parser.options[:screenshots_dirname]
assert_equal "OUTPUT.md", parser.options[:output_filename]
assert_equal true, parser.options[:force]

expected = [
:on_success_dir,
:on_failure_dir,
:dotenv_path,
:screenshots_dirname,
:output_filename,
:force,
:io,
:source_path
]
assert_equal expected.sort, parser.options.keys.sort
end
end
end
end
12 changes: 6 additions & 6 deletions test/rundoc/peg_parser_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -270,36 +270,36 @@ def test_no_args

def test_rundoc_sub_commands_no_quotes
input = +""
input << %(:::-- rundoc.depend_on ../foo/bar.md\n)
input << %(:::-- rundoc.require ../foo/bar.md\n)

parser = Rundoc::PegParser.new.command_with_stdin
tree = parser.parse_with_debug(input)

actual = @transformer.apply(tree)
assert_equal :"rundoc.depend_on", actual.keyword
assert_equal :"rundoc.require", actual.keyword
end

def test_seattle_style_method_call
input = +""
input << %(rundoc.depend_on '../foo/bar.md')
input << %(rundoc.require '../foo/bar.md')
parser = Rundoc::PegParser.new.method_call
tree = parser.parse_with_debug(input)

actual = @transformer.apply(tree)

assert_equal :"rundoc.depend_on", actual.keyword
assert_equal :"rundoc.require", actual.keyword
end

def test_rundoc_seattle_sub_command
input = +""
input << %(:::>> rundoc.depend_on '../foo/bar.md'\n)
input << %(:::>> rundoc.require '../foo/bar.md'\n)

parser = Rundoc::PegParser.new.command
tree = parser.parse_with_debug(input)

actual = @transformer.apply(tree)

assert_equal :"rundoc.depend_on", actual.keyword
assert_equal :"rundoc.require", actual.keyword
end

def test_positional_args
Expand Down
26 changes: 22 additions & 4 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,26 @@ class Minitest::Test
def default_context(
output_dir: nil,
source_path: nil,
screenshots_dir: nil
screenshots_dirname: nil
)

Rundoc::Context::Execution.new(
output_dir: output_dir || Pathname("/dev/null"),
source_path: source_path || Pathname("/dev/null"),
screenshots_dir: screenshots_dir || Pathname("/dev/null")
screenshots_dirname: screenshots_dirname || Pathname("/dev/null")
)
end

def parse_contents(
contents,
output_dir: nil,
source_path: nil,
screenshots_dir: nil
screenshots_dirname: nil
)
context = default_context(
output_dir: output_dir,
source_path: source_path,
screenshots_dir: screenshots_dir
screenshots_dirname: screenshots_dirname
)
Rundoc::Parser.new(
contents,
Expand All @@ -63,4 +63,22 @@ def strip_autogen_warning(string)
string.gsub!(/<!-- STOP.*STOP -->/m, "")
string
end

class FakeExit
def initialize
@called = false
@value = nil
end

def exit(value = nil)
@called = true
@value = value
end

def called?
@called
end

attr_reader :value
end
end

0 comments on commit 710e88c

Please sign in to comment.