From abfa78889171571bd6781c60a3c201b1715d807f Mon Sep 17 00:00:00 2001 From: Nobuyoshi Nakada Date: Wed, 17 Feb 2021 14:50:53 +0900 Subject: [PATCH] Add -C/--directory option the same as GNU make --- lib/rake/application.rb | 7 +++++++ test/test_rake_application_options.rb | 22 ++++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/lib/rake/application.rb b/lib/rake/application.rb index 9ac9b2130..02586ad5e 100644 --- a/lib/rake/application.rb +++ b/lib/rake/application.rb @@ -433,6 +433,13 @@ def standard_rake_options # :nodoc: select_tasks_to_show(options, :describe, value) } ], + ["--directory", "-C [DIRECTORY]", + "Change to DIRECTORY before doing anything.", + lambda { |value| + Dir.chdir value + @original_dir = Dir.pwd + } + ], ["--dry-run", "-n", "Do a dry run without executing actions.", lambda { |value| diff --git a/test/test_rake_application_options.rb b/test/test_rake_application_options.rb index 0ca06e264..288085938 100644 --- a/test/test_rake_application_options.rb +++ b/test/test_rake_application_options.rb @@ -65,6 +65,28 @@ def test_describe_with_pattern end end + def test_directory + pwd = Dir.pwd + [["--directory"], "--directory=", ["-C"], "-C"].each do |flag| + Dir.mktmpdir do |dir| + begin + flags(flag.dup << dir) do + assert_equal(File.realpath(dir), @app.original_dir) + end + ensure + Dir.chdir(pwd) + end + begin + assert_raises(Errno::ENOENT) do + flags(flag.dup << dir+"/nonexistent") + end + ensure + Dir.chdir(pwd) + end + end + end + end + def test_execute $xyzzy = 0 flags("--execute=$xyzzy=1", "-e $xyzzy=1") do