From e29ec5a126b6da617f4e8c9913d91264040c5ecf Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Tue, 30 Oct 2018 17:07:44 -0400 Subject: [PATCH] move clean logic to Application --- lib/rscons.rb | 19 ------------------- lib/rscons/application.rb | 23 +++++++++++++++++++++++ spec/rscons/application_spec.rb | 25 +++++++++++++++++++++++++ spec/rscons_spec.rb | 25 ------------------------- 4 files changed, 48 insertions(+), 44 deletions(-) diff --git a/lib/rscons.rb b/lib/rscons.rb index eba02c6..7d79514 100644 --- a/lib/rscons.rb +++ b/lib/rscons.rb @@ -57,25 +57,6 @@ module Rscons @application ||= Application.new end - # Remove all generated files. - # - # @return [void] - def clean - cache = Cache.instance - # remove all built files - cache.targets.each do |target| - FileUtils.rm_f(target) - end - # remove all created directories if they are empty - cache.directories.sort {|a, b| b.size <=> a.size}.each do |directory| - next unless File.directory?(directory) - if (Dir.entries(directory) - ['.', '..']).empty? - Dir.rmdir(directory) rescue nil - end - end - cache.clear - end - # Return whether the given path is an absolute filesystem path. # # @param path [String] the path to examine. diff --git a/lib/rscons/application.rb b/lib/rscons/application.rb index 4f4caae..1ec38d4 100644 --- a/lib/rscons/application.rb +++ b/lib/rscons/application.rb @@ -29,11 +29,34 @@ module Rscons # Process exit code (0 on success). def run(operation) # TODO + case operation + when "clean" + clean + end 0 end private + # Remove all generated files. + # + # @return [void] + def clean + cache = Cache.instance + # remove all built files + cache.targets.each do |target| + FileUtils.rm_f(target) + end + # remove all created directories if they are empty + cache.directories.sort {|a, b| b.size <=> a.size}.each do |directory| + next unless File.directory?(directory) + if (Dir.entries(directory) - ['.', '..']).empty? + Dir.rmdir(directory) rescue nil + end + end + cache.clear + end + # Determine the number of threads to use by default. # # @return [Integer] diff --git a/spec/rscons/application_spec.rb b/spec/rscons/application_spec.rb index ab45b42..fe04393 100644 --- a/spec/rscons/application_spec.rb +++ b/spec/rscons/application_spec.rb @@ -1,6 +1,31 @@ module Rscons describe Application do + describe ".clean" do + it "removes all build targets and created directories" do + cache = "cache" + expect(Rscons::Cache).to receive(:instance).and_return(cache) + expect(cache).to receive(:targets).and_return(["build/a.out", "build/main.o"]) + expect(FileUtils).to receive(:rm_f).with("build/a.out") + expect(FileUtils).to receive(:rm_f).with("build/main.o") + expect(cache).to receive(:directories).and_return(["build/one", "build/one/two", "build", "other"]) + expect(File).to receive(:directory?).with("build/one/two").and_return(true) + expect(Dir).to receive(:entries).with("build/one/two").and_return([".", ".."]) + expect(Dir).to receive(:rmdir).with("build/one/two") + expect(File).to receive(:directory?).with("build/one").and_return(true) + expect(Dir).to receive(:entries).with("build/one").and_return([".", ".."]) + expect(Dir).to receive(:rmdir).with("build/one") + expect(File).to receive(:directory?).with("build").and_return(true) + expect(Dir).to receive(:entries).with("build").and_return([".", ".."]) + expect(Dir).to receive(:rmdir).with("build") + expect(File).to receive(:directory?).with("other").and_return(true) + expect(Dir).to receive(:entries).with("other").and_return([".", "..", "other.file"]) + expect(cache).to receive(:clear) + + Rscons.application.__send__(:clean) + end + end + describe ".determine_n_threads" do context "when specified by environment variable" do before(:each) do diff --git a/spec/rscons_spec.rb b/spec/rscons_spec.rb index 767e3d5..7627237 100644 --- a/spec/rscons_spec.rb +++ b/spec/rscons_spec.rb @@ -1,29 +1,4 @@ describe Rscons do - describe ".clean" do - it "removes all build targets and created directories" do - cache = "cache" - expect(Rscons::Cache).to receive(:instance).and_return(cache) - expect(cache).to receive(:targets).and_return(["build/a.out", "build/main.o"]) - expect(FileUtils).to receive(:rm_f).with("build/a.out") - expect(FileUtils).to receive(:rm_f).with("build/main.o") - expect(cache).to receive(:directories).and_return(["build/one", "build/one/two", "build", "other"]) - expect(File).to receive(:directory?).with("build/one/two").and_return(true) - expect(Dir).to receive(:entries).with("build/one/two").and_return([".", ".."]) - expect(Dir).to receive(:rmdir).with("build/one/two") - expect(File).to receive(:directory?).with("build/one").and_return(true) - expect(Dir).to receive(:entries).with("build/one").and_return([".", ".."]) - expect(Dir).to receive(:rmdir).with("build/one") - expect(File).to receive(:directory?).with("build").and_return(true) - expect(Dir).to receive(:entries).with("build").and_return([".", ".."]) - expect(Dir).to receive(:rmdir).with("build") - expect(File).to receive(:directory?).with("other").and_return(true) - expect(Dir).to receive(:entries).with("other").and_return([".", "..", "other.file"]) - expect(cache).to receive(:clear) - - Rscons.clean - end - end - describe ".absolute_path?" do context "on Windows" do it "returns whether a path is absolute" do