From 4c6a8e9bdf71d9eeb4ea320db67647086add2324 Mon Sep 17 00:00:00 2001 From: Thomas Sawyer Date: Sun, 29 Nov 2026 13:30:35 -0500 Subject: [PATCH 1/2] Bring back the QED demonstrations The Shell, Script and Batch specs were dropped when Ratch was revived for Ruby 3 (2.0.0). They return as demo/, passing against the rewritten Shell with little change: listings are in sorted order, and the setup clears QED's temporary directory itself. The Shell demonstration gains sections on standing at another directory, noop mode and system calls, in place of a pointer to a System class that is gone. The empty System spec is not restored. CI runs them with the unit tests. Co-Authored-By: Claude Fable 5.2 --- .github/workflows/ci.yml | 2 +- HISTORY.rdoc | 7 ++ README.rdoc | 9 ++ Rakefile | 7 +- demo/01_shell.rdoc | 239 +++++++++++++++++++++++++++++++++++++++ demo/02_script.rdoc | 34 ++++++ demo/03_batch.rdoc | 71 ++++++++++++ demo/applique/array.rb | 8 ++ demo/applique/setup.rb | 29 +++++ ratch.gemspec | 2 + 10 files changed, 406 insertions(+), 2 deletions(-) create mode 100644 demo/01_shell.rdoc create mode 100644 demo/02_script.rdoc create mode 100644 demo/03_batch.rdoc create mode 100644 demo/applique/array.rb create mode 100644 demo/applique/setup.rb diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 8d5fbaa..acf5c9e 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -27,4 +27,4 @@ jobs: ruby-version: ${{ matrix.ruby }} bundler-cache: true - name: Tests - run: bundle exec rake test + run: bundle exec rake test demo diff --git a/HISTORY.rdoc b/HISTORY.rdoc index abc19a2..85fe3df 100644 --- a/HISTORY.rdoc +++ b/HISTORY.rdoc @@ -1,5 +1,12 @@ = RELEASE HISTORY +== Unreleased + +* Bring back the QED demonstrations of Shell, Script and Batch, which + were dropped in 2.0.0. They are in demo/, brought up to date, + and run in CI. The Shell one gains sections on standing at another + directory, noop mode and system calls. + == 2.1.0 | 2026-11-25 This release takes in what was worth keeping from ProUtils, a later diff --git a/README.rdoc b/README.rdoc index 7e255a5..46073d5 100644 --- a/README.rdoc +++ b/README.rdoc @@ -153,6 +153,15 @@ the directory it is in. $ ludo script/test --verbose # runs project/script/test +== Demonstrations + +The +demo+ directory has walkthroughs of Shell, Script and Batch. They +are QED[https://github.com/rubyworks/qed] documents, so each example in +them is run and checked: + + $ bundle exec rake demo + + == Installation $ gem install ratch diff --git a/Rakefile b/Rakefile index c99915c..5319641 100644 --- a/Rakefile +++ b/Rakefile @@ -18,4 +18,9 @@ task 'test' do sh 'rubytest -Ilib -r spectrum/testcase -r ./test/helper test/case_*.rb' end -task 'default' => 'test' +desc 'run the QED demonstrations' +task 'demo' do + sh 'qed -Ilib demo' +end + +task 'default' => ['test', 'demo'] diff --git a/demo/01_shell.rdoc b/demo/01_shell.rdoc new file mode 100644 index 0000000..086a5dd --- /dev/null +++ b/demo/01_shell.rdoc @@ -0,0 +1,239 @@ += Ratch::Shell + +The Shell class mimics a basic shell command line prompt. + + require 'ratch/shell' + +The demonstration sets up sample directory consisting of the following +entries in a temporary directory: + + foo.txt + zoo/ + zoo/bar.txt + +== Initial Path + +When no path argument is provided to the initializer, the present working path +is used. The path is stored as a Pathname object. + + shell = Ratch::Shell.new + shell.work.assert = Pathname.new(Dir.pwd).expand_path + +Otherwise the path given is used. + + shell = Ratch::Shell.new('.') + shell.work.assert = Pathname.new('.').expand_path + + shell = Ratch::Shell.new('..') + shell.work.assert = Pathname.new('..').expand_path + +An error is raised if the path does not exist. + + expect Ratch::FileNotFound do + Ratch::Shell.new('not_a_path') + end + +== Processing Options + +The +initialize+ method accepts a few options. The +quiet+ option suppresses +all output to stdout. + + shell = Ratch::Shell.new(:quiet=>true) + shell.assert.quiet? + +The +noop+ option prevents any actual writing to disk --the process will +simply pretend that it has done so. + + shell = Ratch::Shell.new(:noop=>true) + shell.assert.noop? + +The +trace+ option provides step by step feedback on what is taking place. + + shell = Ratch::Shell.new(:trace=>true) + shell.assert.trace? + +The +dryrun+ option is simply a combination of +noop+ and +trace+. + + shell = Ratch::Shell.new(:dryrun=>true) + shell.assert.noop? + shell.assert.trace? + shell.assert.dryrun? + +== Equality + +For two Shell objects to be considered equal, via #==, they must both +be instances of Ratch::Shell (or subclass) and have the same working +path. + + shell1 = Ratch::Shell.new('.') + shell2 = Ratch::Shell.new('.') + shell1.assert = shell2 + + shell1 = Ratch::Shell.new('.') + shell2 = Ratch::Shell.new('..') + shell1.refute = shell2 + +The more strict #eql? method also ensures that the +noop+ option is the same. + + shell1 = Ratch::Shell.new('.', :noop=>true) + shell2 = Ratch::Shell.new('.', :noop=>true) + shell1.assert.eql? shell2 + + shell1 = Ratch::Shell.new('.', :noop=>true) + shell2 = Ratch::Shell.new('.', :noop=>false) + shell1.refute.eql? shell2 + +== File System Locations + +The Shell class provides a few convenient methods for accessing common +locations in the file system, namely #work, #home, #parent and #root. + + shell = Ratch::Shell.new + + shell.work.assert == Pathname.new('.').expand_path + shell.parent.assert == Pathname.new('..').expand_path + +The current user's home directory is accessible via the #home method. + + shell.home.assert == Pathname.new('~').expand_path + +The system's root folder can be accessed via #root method. + + shell.root.assert == Pathname.new('/').expand_path + +In addition the Shell class provides methods for accessing pathnames +relative to the current shell directory. The #path (or #pathname) method +returns a Pathname object with the given path. + + shell.path('foo') + +This will work regardless if the path actually exists or not. On the other hand, +the #file and #dir methods will do the same, but will raise an error if the path +given is not an existing file or a directory, respectively. + + shell.file('foo.txt') + + expect Ratch::FileNotFound do + shell.file('not.txt') + end + + shell.dir('zoo') + + expect Ratch::FileNotFound do + shell.dir('not') + end + +== Entries + +Shell#entries works just like Dir.entries except that each path is returned +as a Pathname object. + + shell = Ratch::Shell.new + + shell.entries.assert == ['.', '..', 'foo.txt', 'zoo'].map{|f| Pathname.new(f)} + +The #file_entries method limits the list to files only. + + shell.file_entries.assert == ['foo.txt'].map{|f| Pathname.new(f)} + +Whereas #directory_entries (or #dir_entries) limits the list to directories. + + shell.directory_entries.assert == ['.', '..', 'zoo'].map{|f| Pathname.new(f)} + +Of course, having to deal with the '.' and '..' is annoying most of the time +so Shell provides more convenient methods. Like #entries, #pathnames +provides a list of entries less the dot paths. + + shell.pathnames.assert == ['foo.txt', 'zoo'].map{|f| Pathname.new(f)} + +The #directories (also #folders) method limits this list to directories only. + + shell.directories.assert == ['zoo'].map{|f| Pathname.new(f)} + +And #files works out to be the same as #file_entries. + + shell.files.assert == ['foo.txt'].map{|f| Pathname.new(f)} + +== Globbing + + shell = Ratch::Shell.new + + shell.glob('*').assert == ['foo.txt', 'zoo'] + +== File Testing + +Shell provides an interface to most of FileTest's functions. + + shell.assert.exist?('foo.txt') + shell.assert.file?('foo.txt') + shell.refute.directory?('foo.txt') + shell.assert.directory?('zoo') + +== File IO + +Shell can be used to easily read the contents of a file. + + shell = Ratch::Shell.new + + shell.read('foo.txt').assert == 'SAMPLE FOO.TXT' + +As well as write a new file. + + shell.write('baz.txt', 'BAZ TEXT') + shell.read('baz.txt').assert == 'BAZ TEXT' + +Or append to an existing file. + + shell.append('baz.txt', ' 2') + shell.read('baz.txt').assert == 'BAZ TEXT 2' + +The shell command supports many file methods, including #rm. + + shell.rm('baz.txt') + +== Standing Elsewhere + +A shell's paths are taken from the directory it stands at, which need not +be the directory the program is running in. The program's own working +directory is left alone. + + zoo = Ratch::Shell.new('zoo') + + zoo.read('bar.txt').assert == 'SAMPLE ZOO/BAR.TXT' + + zoo.files.assert == ['bar.txt'].map{|f| Pathname.new(f)} + + Dir.pwd.assert == shell.work.to_s + +So shells at different directories can be used side by side. + + shell.cp('foo.txt', 'zoo') + + zoo.assert.file?('foo.txt') + +== Pretending + +In +noop+ mode a shell goes through the motions and changes nothing. + + pretend = Ratch::Shell.new(:noop=>true) + + pretend.rm('foo.txt') + + shell.assert.file?('foo.txt') + +== System Calls + +The Shell class can also call out to system commands, with #sh. They +run from the shell's directory. It returns true if the command succeeded. + + zoo.sh('echo hello > out.txt').assert == true + + zoo.read('out.txt').assert == "hello\n" + + zoo.sh('exit 1').assert == false + +A command can also be called as a method of #cmd. + + zoo.cmd.touch('made.txt') + + zoo.assert.file?('made.txt') diff --git a/demo/02_script.rdoc b/demo/02_script.rdoc new file mode 100644 index 0000000..d6d88d7 --- /dev/null +++ b/demo/02_script.rdoc @@ -0,0 +1,34 @@ += Ratch::Script + +The Ratch::Script class is the context used for evaluating Ratch-based +batch files, aka shell scripts. + + require 'ratch/script' + +In most every respect the Script class behaves like Ratch::Shell, except that +its initializer takes a file name to be evaluated. + +Let's say we have a Ratch script called 'foo.ratch': + + #!/usr/bin/env ratch + + @test_message = "Hello World!" + +We can load the script via Ratch::Script.new. + + script = Ratch::Script.new('foo.ratch') + +The file name can be accessed from the script using #script_file. + + script.script_file.assert == 'foo.ratch' + +To execute a script, use the `#execute!` method, or its alias `#run!`. + + script.execute! + +We can see that the script did indeed execute by looking for the instance +variable it set. + + msg = script.instance_variable_get('@test_message') + msg.assert == "Hello World!" + diff --git a/demo/03_batch.rdoc b/demo/03_batch.rdoc new file mode 100644 index 0000000..1c6d4ad --- /dev/null +++ b/demo/03_batch.rdoc @@ -0,0 +1,71 @@ += Ratch::Batch + +The Batch class makes it possible to work with multiple files +at once as easily as one would work with a single file. + + require 'ratch/batch' + +The demonstration sets up a sample directory consisting of the following +entries in a temporary directory: + + foo.txt + bar.txt + zoo/ + zoo/one.txt + zoo/two.txt + +To create a new Batch instance, pass the base directory and any +inclusion patterns to the initializer. + + batch = Ratch::Batch.new('.', '*.txt') + +In this example we will get every `.txt` file at the toplevel of the base +directory. + + batch.to_a.assert = ['bar.txt', 'foo.txt'].to_pathnames + +To get a list of files relative to the base directory, use #entries. + + batch.entries.assert = ['bar.txt', 'foo.txt'].to_pathnames + +Internally a Batch instance tracks the files selected via a Ratch::FileList +object. This can be directly accessed via the #list method. + + batch.file_list.assert.is_a?(Ratch::FileList) + + batch.file_list.to_a.assert = ['bar.txt', 'foo.txt'] + +As with the delegated FileTest methods of the Shell class, Batch can test +the set of files in aggregate. For example, to ensure all the entries +are files we can use the #file? method. + + batch.assert.file? + +Likewise to ensure none of the entries are directories we can use the #directory? +method. + + batch.refute.directory? + +The Batch list can be reduced to just files or just directories via the #file! +and #directory! methods. + + batch = Ratch::Batch.new('.', '*') + batch.file! + batch.list.assert = ['bar.txt', 'foo.txt'] + + batch = Ratch::Batch.new('.', '*') + batch.directory! + batch.list.assert = ['zoo'] + +The Batch class is enumerable, both #each and #size are defined. + + batch = Ratch::Batch.new('.', '*.txt') + + batch.each do |pathname| + pathname.assert.is_a?(Pathname) + end + + batch.size.assert = 2 + +Any enumerable method is likewise applicable. + diff --git a/demo/applique/array.rb b/demo/applique/array.rb new file mode 100644 index 0000000..0c7c01f --- /dev/null +++ b/demo/applique/array.rb @@ -0,0 +1,8 @@ +class ::Array + + # Helper extension for comparison tests. + def to_pathnames + map{ |f| Pathname.new(f) } + end + +end diff --git a/demo/applique/setup.rb b/demo/applique/setup.rb new file mode 100644 index 0000000..9773de2 --- /dev/null +++ b/demo/applique/setup.rb @@ -0,0 +1,29 @@ +require 'tmpdir' +require 'ae' +require 'fileutils' +require 'pathname' + +# Each demonstration starts in an empty directory. QED runs them in a +# temporary directory of its own (tmp/qed), which is checked before +# anything is removed. +Before :demo do + here = Dir.pwd + unless here.split('/').include?('qed') || here.start_with?(Dir.tmpdir) + abort "Not in QED's temporary directory: #{here}" + end + Dir.children(here).each{ |entry| FileUtils.rm_rf(File.join(here, entry)) } +end + +When "consisting of the following entries" do |text| + text.split(/\s+/).each do |file| + if /\/$/ =~ file + FileUtils.mkdir_p(file) unless File.directory?(file) + else + File.open(file, 'w+'){ |f| f << "SAMPLE #{file}".upcase } + end + end +end + +When "Let's say we have a Ratch script called '(((.*?)))'" do |file, text| + File.open(file, 'w'){ |f| f << text } +end diff --git a/ratch.gemspec b/ratch.gemspec index e78f114..22c613e 100644 --- a/ratch.gemspec +++ b/ratch.gemspec @@ -44,4 +44,6 @@ Gem::Specification.new do |s| s.add_development_dependency 'rubytest', '>= 0.11.1' s.add_development_dependency 'spectrum', '>= 0.2' s.add_development_dependency 'assay', '>= 0.5.1' + s.add_development_dependency 'qed', '>= 3.0' + s.add_development_dependency 'ae', '>= 1.9' end From 56ed34a7680db66d0ba06044d5f16f0b323de2cd Mon Sep 17 00:00:00 2001 From: Thomas Sawyer Date: Sun, 29 Nov 2026 13:31:36 -0500 Subject: [PATCH 2/2] Sort directory entries in the Shell demonstration Their order is the file system's, which differs in CI. Co-Authored-By: Claude Fable 5.2 --- demo/01_shell.rdoc | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/demo/01_shell.rdoc b/demo/01_shell.rdoc index 086a5dd..f996a0e 100644 --- a/demo/01_shell.rdoc +++ b/demo/01_shell.rdoc @@ -126,11 +126,12 @@ given is not an existing file or a directory, respectively. == Entries Shell#entries works just like Dir.entries except that each path is returned -as a Pathname object. +as a Pathname object. As with Dir.entries, the order is the file system's +own, so we sort them here. shell = Ratch::Shell.new - shell.entries.assert == ['.', '..', 'foo.txt', 'zoo'].map{|f| Pathname.new(f)} + shell.entries.sort.assert == ['.', '..', 'foo.txt', 'zoo'].map{|f| Pathname.new(f)} The #file_entries method limits the list to files only. @@ -138,13 +139,13 @@ The #file_entries method limits the list to files only. Whereas #directory_entries (or #dir_entries) limits the list to directories. - shell.directory_entries.assert == ['.', '..', 'zoo'].map{|f| Pathname.new(f)} + shell.directory_entries.sort.assert == ['.', '..', 'zoo'].map{|f| Pathname.new(f)} Of course, having to deal with the '.' and '..' is annoying most of the time so Shell provides more convenient methods. Like #entries, #pathnames provides a list of entries less the dot paths. - shell.pathnames.assert == ['foo.txt', 'zoo'].map{|f| Pathname.new(f)} + shell.pathnames.sort.assert == ['foo.txt', 'zoo'].map{|f| Pathname.new(f)} The #directories (also #folders) method limits this list to directories only.