|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require "test_helper" |
| 4 | +require "json" |
| 5 | +require "rbconfig" |
| 6 | +require "mcp/client/stdio" |
| 7 | + |
| 8 | +module MCP |
| 9 | + class Client |
| 10 | + class StdioEnvironmentTest < Minitest::Test |
| 11 | + def test_default_environment_does_not_inherit_unrelated_variables |
| 12 | + with_environment("MCP_TEST_HOST_ONLY" => "host-value") do |
| 13 | + refute(server_environment.key?("MCP_TEST_HOST_ONLY")) |
| 14 | + end |
| 15 | + end |
| 16 | + |
| 17 | + def test_empty_env_does_not_inherit_unrelated_variables |
| 18 | + with_environment("MCP_TEST_HOST_ONLY" => "host-value") do |
| 19 | + refute(server_environment(env: {}).key?("MCP_TEST_HOST_ONLY")) |
| 20 | + end |
| 21 | + end |
| 22 | + |
| 23 | + def test_explicit_env_does_not_inherit_unrelated_variables |
| 24 | + with_environment("MCP_TEST_HOST_ONLY" => "host-value") do |
| 25 | + environment = server_environment(env: { "MCP_TEST_SERVER_SETTING" => "server-value" }) |
| 26 | + |
| 27 | + assert_equal("server-value", environment["MCP_TEST_SERVER_SETTING"]) |
| 28 | + refute(environment.key?("MCP_TEST_HOST_ONLY")) |
| 29 | + end |
| 30 | + end |
| 31 | + |
| 32 | + def test_default_environment_preserves_path_and_home |
| 33 | + home_variable = Gem.win_platform? ? "USERPROFILE" : "HOME" |
| 34 | + with_environment(home_variable => "server-home") do |
| 35 | + environment = server_environment |
| 36 | + |
| 37 | + assert_equal("server-home", environment[home_variable]) |
| 38 | + assert_equal(ENV.fetch("PATH"), environment["PATH"]) |
| 39 | + end |
| 40 | + end |
| 41 | + |
| 42 | + def test_default_environment_preserves_ruby_configuration |
| 43 | + variables = { |
| 44 | + "GEM_HOME" => Gem.dir, |
| 45 | + "GEM_PATH" => Gem.path.join(File::PATH_SEPARATOR), |
| 46 | + } |
| 47 | + with_environment(variables) do |
| 48 | + assert_equal(variables, server_environment.slice(*variables.keys)) |
| 49 | + end |
| 50 | + end |
| 51 | + |
| 52 | + def test_default_environment_does_not_inherit_bundle_gemfile |
| 53 | + with_environment("BUNDLE_GEMFILE" => File.expand_path("client/Gemfile")) do |
| 54 | + refute(server_environment.key?("BUNDLE_GEMFILE")) |
| 55 | + end |
| 56 | + end |
| 57 | + |
| 58 | + def test_explicit_env_can_select_bundle_gemfile |
| 59 | + gemfile = File.expand_path("server/Gemfile") |
| 60 | + with_environment("BUNDLE_GEMFILE" => File.expand_path("client/Gemfile")) do |
| 61 | + environment = server_environment(env: { "BUNDLE_GEMFILE" => gemfile }) |
| 62 | + |
| 63 | + assert_equal(gemfile, environment["BUNDLE_GEMFILE"]) |
| 64 | + end |
| 65 | + end |
| 66 | + |
| 67 | + def test_default_environment_preserves_case_insensitive_variable_lookups |
| 68 | + # Windows retains mixed-case names in ENV.to_h but accepts uppercase ENV lookups. |
| 69 | + ENV.stubs(:to_h).returns("Path" => "server-path") |
| 70 | + ENV.stubs(:[]).returns(nil) |
| 71 | + ENV.stubs(:[]).with("PATH").returns("server-path") |
| 72 | + |
| 73 | + assert_equal("server-path", server_environment["PATH"]) |
| 74 | + end |
| 75 | + |
| 76 | + def test_explicit_env_overrides_default_variables |
| 77 | + environment = server_environment(env: { "PATH" => "server-path" }) |
| 78 | + |
| 79 | + assert_equal("server-path", environment["PATH"]) |
| 80 | + end |
| 81 | + |
| 82 | + def test_explicit_nil_removes_a_default_variable |
| 83 | + with_environment("PATH" => "parent-path") do |
| 84 | + refute(server_environment(env: { "PATH" => nil }).key?("PATH")) |
| 85 | + end |
| 86 | + end |
| 87 | + |
| 88 | + def test_missing_default_variable_is_not_added |
| 89 | + with_environment("TERM" => nil) do |
| 90 | + refute(server_environment.key?("TERM")) |
| 91 | + end |
| 92 | + end |
| 93 | + |
| 94 | + def test_inherit_env_restores_full_environment_inheritance |
| 95 | + with_environment("MCP_TEST_HOST_ONLY" => "host-value") do |
| 96 | + environment = server_environment(inherit_env: true) |
| 97 | + |
| 98 | + assert_equal("host-value", environment["MCP_TEST_HOST_ONLY"]) |
| 99 | + end |
| 100 | + end |
| 101 | + |
| 102 | + def test_explicit_env_overrides_and_removes_variables_when_inheriting |
| 103 | + with_environment("MCP_TEST_HOST_ONLY" => "host-value", "MCP_TEST_REMOVE" => "remove-value") do |
| 104 | + environment = server_environment( |
| 105 | + inherit_env: true, |
| 106 | + env: { "MCP_TEST_HOST_ONLY" => "server-value", "MCP_TEST_REMOVE" => nil }, |
| 107 | + ) |
| 108 | + |
| 109 | + assert_equal("server-value", environment["MCP_TEST_HOST_ONLY"]) |
| 110 | + refute(environment.key?("MCP_TEST_REMOVE")) |
| 111 | + end |
| 112 | + end |
| 113 | + |
| 114 | + private |
| 115 | + |
| 116 | + def server_environment(**options) |
| 117 | + transport = Stdio.new( |
| 118 | + command: RbConfig.ruby, |
| 119 | + args: ["-e", "require 'json'; puts JSON.generate(ENV.to_h)"], |
| 120 | + **options, |
| 121 | + ) |
| 122 | + transport.start |
| 123 | + output = Timeout.timeout(5) { transport.instance_variable_get(:@stdout).read } |
| 124 | + JSON.parse(output) |
| 125 | + ensure |
| 126 | + transport&.close |
| 127 | + end |
| 128 | + |
| 129 | + def with_environment(variables) |
| 130 | + previous_values = variables.to_h { |key, _| [key, ENV[key]] } |
| 131 | + variables.each { |key, value| ENV[key] = value } |
| 132 | + yield |
| 133 | + ensure |
| 134 | + previous_values.each { |key, value| ENV[key] = value } |
| 135 | + end |
| 136 | + end |
| 137 | + end |
| 138 | +end |
0 commit comments