From 66eb27eb145c7f52880afc40de2b381ecb572ff6 Mon Sep 17 00:00:00 2001 From: "Brian J. Cardiff" Date: Fri, 30 Aug 2024 10:43:53 -0300 Subject: [PATCH 1/7] Adds initial support for external commands --- src/compiler/crystal/command.cr | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/compiler/crystal/command.cr b/src/compiler/crystal/command.cr index f8ece87e3d4b..56dea7c7bcbf 100644 --- a/src/compiler/crystal/command.cr +++ b/src/compiler/crystal/command.cr @@ -130,6 +130,8 @@ class Crystal::Command else if command.ends_with?(".cr") error "file '#{command}' does not exist" + elsif external_command = Process.find_executable("crystal-#{command}") + Process.exec(external_command, options, env: {"CRYSTAL" => Process.executable_path}) else error "unknown command: #{command}" end From f3818aa6a69731ad0ee25e51afaf3404f1ab2b12 Mon Sep 17 00:00:00 2001 From: "Brian J. Cardiff" Date: Tue, 3 Sep 2024 12:44:28 -0300 Subject: [PATCH 2/7] Drop external command from forwarded ARGV --- src/compiler/crystal/command.cr | 1 + 1 file changed, 1 insertion(+) diff --git a/src/compiler/crystal/command.cr b/src/compiler/crystal/command.cr index 56dea7c7bcbf..1354594706fb 100644 --- a/src/compiler/crystal/command.cr +++ b/src/compiler/crystal/command.cr @@ -131,6 +131,7 @@ class Crystal::Command if command.ends_with?(".cr") error "file '#{command}' does not exist" elsif external_command = Process.find_executable("crystal-#{command}") + options.shift Process.exec(external_command, options, env: {"CRYSTAL" => Process.executable_path}) else error "unknown command: #{command}" From 7537f61da66ca01f3050b3ac2b87422460786b17 Mon Sep 17 00:00:00 2001 From: "Brian J. Cardiff" Date: Tue, 3 Sep 2024 12:44:41 -0300 Subject: [PATCH 3/7] Add spec --- .../crystal/commands/external_command_spec.cr | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 spec/compiler/crystal/commands/external_command_spec.cr diff --git a/spec/compiler/crystal/commands/external_command_spec.cr b/spec/compiler/crystal/commands/external_command_spec.cr new file mode 100644 index 000000000000..4faeb65dd71c --- /dev/null +++ b/spec/compiler/crystal/commands/external_command_spec.cr @@ -0,0 +1,32 @@ +require "../../../spec_helper" + +describe Crystal::Command do + it "exec external commands" do + with_temp_executable "crystal-external" do |path| + with_tempfile "crystal-external.cr" do |source_file| + File.write source_file, <<-CRYSTAL + puts ENV["CRYSTAL"]? + puts PROGRAM_NAME + puts ARGV + CRYSTAL + + Process.run(ENV["CRYSTAL_SPEC_COMPILER_BIN"]? || "bin/crystal", ["build", source_file, "-o", path]) + end + + File.exists?(path).should be_true + + process = Process.new(ENV["CRYSTAL_SPEC_COMPILER_BIN"]? || "bin/crystal", + ["external", "foo", "bar"], + output: :pipe, + env: {"PATH" => File.dirname(path)} + ) + output = process.output.gets_to_end + status = process.wait + status.success?.should be_true + lines = output.lines + lines[0].should match /crystal/ + lines[1].should match /crystal-external/ + lines[2].should eq %(["foo", "bar"]) + end + end +end From 9e4d2bd5fe33323c2383075081c196245e49f22b Mon Sep 17 00:00:00 2001 From: "Brian J. Cardiff" Date: Tue, 3 Sep 2024 13:38:38 -0300 Subject: [PATCH 4/7] Update external_command_spec.cr MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Johannes Müller --- spec/compiler/crystal/commands/external_command_spec.cr | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/compiler/crystal/commands/external_command_spec.cr b/spec/compiler/crystal/commands/external_command_spec.cr index 4faeb65dd71c..ce0711439361 100644 --- a/spec/compiler/crystal/commands/external_command_spec.cr +++ b/spec/compiler/crystal/commands/external_command_spec.cr @@ -1,7 +1,7 @@ require "../../../spec_helper" describe Crystal::Command do - it "exec external commands" do + it "exec external commands", tags: %w[slow] do with_temp_executable "crystal-external" do |path| with_tempfile "crystal-external.cr" do |source_file| File.write source_file, <<-CRYSTAL From 9679326ddc6d34a0c4eba413807ea41b56bf602d Mon Sep 17 00:00:00 2001 From: "Brian J. Cardiff" Date: Wed, 4 Sep 2024 18:23:15 -0300 Subject: [PATCH 5/7] Move spec to primitive --- .../crystal/commands => primitives}/external_command_spec.cr | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename spec/{compiler/crystal/commands => primitives}/external_command_spec.cr (96%) diff --git a/spec/compiler/crystal/commands/external_command_spec.cr b/spec/primitives/external_command_spec.cr similarity index 96% rename from spec/compiler/crystal/commands/external_command_spec.cr rename to spec/primitives/external_command_spec.cr index ce0711439361..92f1a41331ed 100644 --- a/spec/compiler/crystal/commands/external_command_spec.cr +++ b/spec/primitives/external_command_spec.cr @@ -1,4 +1,4 @@ -require "../../../spec_helper" +require "../spec_helper" describe Crystal::Command do it "exec external commands", tags: %w[slow] do From 4dc19bf96224427609a47894e1aff7f9fb73dde3 Mon Sep 17 00:00:00 2001 From: "Brian J. Cardiff" Date: Wed, 4 Sep 2024 23:26:48 -0300 Subject: [PATCH 6/7] Add to path, not just replace it --- spec/primitives/external_command_spec.cr | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/primitives/external_command_spec.cr b/spec/primitives/external_command_spec.cr index 92f1a41331ed..765672cfcce9 100644 --- a/spec/primitives/external_command_spec.cr +++ b/spec/primitives/external_command_spec.cr @@ -18,7 +18,7 @@ describe Crystal::Command do process = Process.new(ENV["CRYSTAL_SPEC_COMPILER_BIN"]? || "bin/crystal", ["external", "foo", "bar"], output: :pipe, - env: {"PATH" => File.dirname(path)} + env: {"PATH" => {ENV["PATH"], File.dirname(path)}.join(Process::PATH_DELIMITER)} ) output = process.output.gets_to_end status = process.wait From 7bde1c213bde93bd00e62cb3ef4ca5c949232834 Mon Sep 17 00:00:00 2001 From: "Brian J. Cardiff" Date: Thu, 5 Sep 2024 09:02:56 -0300 Subject: [PATCH 7/7] Skip test for interpreter (mimic integration spec) --- spec/primitives/external_command_spec.cr | 2 ++ 1 file changed, 2 insertions(+) diff --git a/spec/primitives/external_command_spec.cr b/spec/primitives/external_command_spec.cr index 765672cfcce9..91687f7c2d21 100644 --- a/spec/primitives/external_command_spec.cr +++ b/spec/primitives/external_command_spec.cr @@ -1,3 +1,5 @@ +{% skip_file if flag?(:interpreted) %} + require "../spec_helper" describe Crystal::Command do