Skip to content

Commit

Permalink
Fix TypeError when tomo/config.rb uses frozen string literals (#448)
Browse files Browse the repository at this point in the history
If the `.tomo/config.rb` file declares a setup or deploy task with
`privileged: true` and the file starts with the special `#
frozen_string_literal: true` comment, tomo crashes with the following
error message:

```
TypeError: can't define singleton
    lib/tomo/configuration/dsl/tasks_block.rb:17:in `extend_object'
    lib/tomo/configuration/dsl/tasks_block.rb:17:in `extend'
    lib/tomo/configuration/dsl/tasks_block.rb:17:in `run'
```

This is because tomo is trying to call `.extend` on a frozen string.

Fix by creating a mutable copy of task names before extending them.
  • Loading branch information
mattbrictson authored May 25, 2024
1 parent dfb0ebb commit 129dfad
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 4 deletions.
2 changes: 1 addition & 1 deletion lib/tomo/configuration/dsl/batch_block.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def initialize(batch)
end

def run(task, privileged: false)
task.extend(Runtime::PrivilegedTask) if privileged
task = String.new(task).extend(Runtime::PrivilegedTask) if privileged
@batch << task
self
end
Expand Down
2 changes: 1 addition & 1 deletion lib/tomo/configuration/dsl/tasks_block.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def batch(&)
end

def run(task, privileged: false)
task.extend(Runtime::PrivilegedTask) if privileged
task = String.new(task).extend(Runtime::PrivilegedTask) if privileged
@tasks << task
self
end
Expand Down
2 changes: 1 addition & 1 deletion lib/tomo/host.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def with_log_prefix(prefix)

def to_s
str = user ? "#{user}@#{address}" : address
str << ":#{port}" unless port == 22
str += ":#{port}" unless port == 22
str
end

Expand Down
2 changes: 1 addition & 1 deletion lib/tomo/runtime.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def setup!
end

def run!(task, *args, privileged: false)
task = task.dup.extend(PrivilegedTask) if privileged
task = String.new(task).extend(PrivilegedTask) if privileged
execution_plan_for([task], release: :current, args:).execute
end

Expand Down
22 changes: 22 additions & 0 deletions test/tomo/configuration_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
require "test_helper"

class Tomo::ConfigurationTest < Minitest::Test
include Tomo::Testing::Local

def test_parses_a_config_file_that_contains_frozen_string_literals
in_temp_dir do
FileUtils.mkdir ".tomo"
File.write(".tomo/config.rb", <<~CONFIG)
# frozen_string_literal: true
setup do
run "nginx:setup", privileged: true
end
CONFIG

parsed = Tomo::Configuration.from_config_rb

assert_instance_of(Tomo::Configuration, parsed)
end
end
end

0 comments on commit 129dfad

Please sign in to comment.