Back to Repositories

Validating Command-Line Utility Functions in Kamal

This test suite validates utility functions in Kamal, focusing on command-line argument handling, string manipulation, and sensitive data redaction. The tests ensure robust handling of shell commands and secure processing of sensitive information.

Test Coverage Overview

The test suite provides comprehensive coverage of Kamal’s utility functions, including argument formatting, option handling, and security features.

  • Command-line argument processing with argumentize and optionize methods
  • Sensitive data handling and redaction across different contexts
  • Shell value escaping with special character handling
  • Edge cases for environment variables and regex patterns

Implementation Analysis

The testing approach uses ActiveSupport::TestCase with minitest assertions to verify utility function behavior. Tests employ pattern matching and string manipulation verification.

  • Assertion-based testing with assert_equal and assert_kind_of
  • Multiple test cases for each utility function
  • Specific handling of edge cases and security concerns

Technical Details

  • Framework: Minitest with ActiveSupport
  • Testing Tools: SSHKit for command execution
  • Configuration: Standard Ruby test helper setup
  • Dependencies: YAML for serialization testing

Best Practices Demonstrated

The test suite exemplifies strong testing practices through focused test cases and comprehensive coverage of functionality.

  • Single responsibility principle in test methods
  • Clear test naming conventions
  • Thorough security testing
  • Comprehensive edge case coverage

basecamp/kamal

test/utils_test.rb

            
require "test_helper"

class UtilsTest < ActiveSupport::TestCase
  test "argumentize" do
    assert_equal [ "--label", "foo=\"\\`bar\\`\"", "--label", "baz=\"qux\"", "--label", :quux, "--label", "quuz=false" ], \
      Kamal::Utils.argumentize("--label", { foo: "`bar`", baz: "qux", quux: nil, quuz: false })
  end

  test "argumentize with redacted" do
    assert_kind_of SSHKit::Redaction, \
      Kamal::Utils.argumentize("--label", { foo: "bar" }, sensitive: true).last
  end

  test "optionize" do
    assert_equal [ "--foo", "\"bar\"", "--baz", "\"qux\"", "--quux" ], \
      Kamal::Utils.optionize({ foo: "bar", baz: "qux", quux: true })
  end

  test "optionize with" do
    assert_equal [ "--foo=\"bar\"", "--baz=\"qux\"", "--quux" ], \
      Kamal::Utils.optionize({ foo: "bar", baz: "qux", quux: true }, with: "=")
  end

  test "no redaction from #to_s" do
    assert_equal "secret", Kamal::Utils.sensitive("secret").to_s
  end

  test "redact from #inspect" do
    assert_equal "[REDACTED]".inspect, Kamal::Utils.sensitive("secret").inspect
  end

  test "redact from SSHKit output" do
    assert_kind_of SSHKit::Redaction, Kamal::Utils.sensitive("secret")
  end

  test "redact from YAML output" do
    assert_equal "--- ! '[REDACTED]'\n", YAML.dump(Kamal::Utils.sensitive("secret"))
  end

  test "escape_shell_value" do
    assert_equal "\"foo\"", Kamal::Utils.escape_shell_value("foo")
    assert_equal "\"\\`foo\\`\"", Kamal::Utils.escape_shell_value("`foo`")

    assert_equal "\"${PWD}\"", Kamal::Utils.escape_shell_value("${PWD}")
    assert_equal "\"${cat /etc/hostname}\"", Kamal::Utils.escape_shell_value("${cat /etc/hostname}")
    assert_equal "\"\\${PWD]\"", Kamal::Utils.escape_shell_value("${PWD]")
    assert_equal "\"\\$(PWD)\"", Kamal::Utils.escape_shell_value("$(PWD)")
    assert_equal "\"\\$PWD\"", Kamal::Utils.escape_shell_value("$PWD")

    assert_equal "\"^(https?://)www.example.com/(.*)\\$\"",
      Kamal::Utils.escape_shell_value("^(https?://)www.example.com/(.*)$")
    assert_equal "\"https://example.com/\\$2\"",
      Kamal::Utils.escape_shell_value("https://example.com/$2")
  end
end