# frozen_string_literal: true require 'minitest/autorun' require 'open3' require 'tmpdir' require 'fileutils' ROOT = File.expand_path('..', __dir__) BIN = File.join(ROOT, 'bin') DEMO = File.join(ROOT, 'demo') # Each test runs in a scratch copy of demo/, so commands that change files # (append, delete, replace ...) never touch the originals. class RdbCase < Minitest::Test # Tests spawn their own processes in their own directory, so they can run # side by side (N=1 in MT_CPU runs them one at a time). parallelize_me! def setup @dir = Dir.mktmpdir('rdb-test') FileUtils.cp_r(Dir[File.join(DEMO, '*')], @dir) end def teardown FileUtils.remove_entry(@dir) end # Run a command through its symlink; returns [stdout, stderr, status]. def run3(cmd, *args, stdin: '', env: {}) Open3.capture3({ 'PATH' => "#{BIN}:#{ENV['PATH']}" }.merge(env), File.join(BIN, cmd), *args, stdin_data: stdin, chdir: @dir) end def rdb(cmd, *args, stdin: nil, file: nil) stdin = File.read(File.join(@dir, file)) if file out, err, st = run3(cmd, *args, stdin: stdin.to_s) assert st.success?, "#{cmd} #{args.join(' ')} failed: #{err}" out end # Output as rows of cells, without the head and dash lines. def data(out) out.lines.drop(2).map { |l| l.chomp.split("\t", -1) } end def read(name) File.read(File.join(@dir, name)) end def write(name, text) File.write(File.join(@dir, name), text) end end