|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require File.expand_path("../test_helper", __dir__) |
| 4 | +require "tmpdir" |
| 5 | + |
| 6 | +module Stripe |
| 7 | + class TelemetryIdTest < Test::Unit::TestCase |
| 8 | + setup do |
| 9 | + TelemetryId.reset! |
| 10 | + end |
| 11 | + |
| 12 | + teardown do |
| 13 | + TelemetryId.reset! |
| 14 | + end |
| 15 | + |
| 16 | + context ".config_dir" do |
| 17 | + if RUBY_PLATFORM !~ /mingw|mswin/ |
| 18 | + should "return XDG_CONFIG_HOME/stripe when XDG_CONFIG_HOME is set" do |
| 19 | + with_env("XDG_CONFIG_HOME" => "/tmp/xdg") do |
| 20 | + assert_equal "/tmp/xdg/stripe", TelemetryId.config_dir |
| 21 | + end |
| 22 | + end |
| 23 | + |
| 24 | + should "return ~/.config/stripe when XDG_CONFIG_HOME is not set" do |
| 25 | + with_env("XDG_CONFIG_HOME" => nil) do |
| 26 | + assert_equal ::File.expand_path("~/.config/stripe"), TelemetryId.config_dir |
| 27 | + end |
| 28 | + end |
| 29 | + |
| 30 | + should "fall back to ~/.config/stripe when XDG_CONFIG_HOME is empty" do |
| 31 | + with_env("XDG_CONFIG_HOME" => "") do |
| 32 | + assert_equal ::File.expand_path("~/.config/stripe"), TelemetryId.config_dir |
| 33 | + end |
| 34 | + end |
| 35 | + else |
| 36 | + should "return APPDATA/Stripe on Windows" do |
| 37 | + refute_nil TelemetryId.config_dir |
| 38 | + assert TelemetryId.config_dir.end_with?("Stripe") |
| 39 | + end |
| 40 | + end |
| 41 | + end |
| 42 | + |
| 43 | + context ".get" do |
| 44 | + should "return nil when config_dir returns nil" do |
| 45 | + TelemetryId.stubs(:config_dir).returns(nil) |
| 46 | + assert_nil TelemetryId.get |
| 47 | + end |
| 48 | + |
| 49 | + should "return cached id from file when it exists" do |
| 50 | + ::Dir.mktmpdir do |dir| |
| 51 | + file_path = ::File.join(dir, "telemetry_id") |
| 52 | + ::File.write(file_path, "stored_id_abc123\n") |
| 53 | + |
| 54 | + TelemetryId.stubs(:config_dir).returns(dir) |
| 55 | + assert_equal "stored_id_abc123", TelemetryId.get |
| 56 | + end |
| 57 | + end |
| 58 | + |
| 59 | + should "generate and persist a new id when file does not exist" do |
| 60 | + ::Dir.mktmpdir do |dir| |
| 61 | + TelemetryId.stubs(:config_dir).returns(dir) |
| 62 | + id = TelemetryId.get |
| 63 | + refute_nil id |
| 64 | + assert_equal 32, id.length |
| 65 | + assert_equal id, ::File.read(::File.join(dir, "telemetry_id")) |
| 66 | + end |
| 67 | + end |
| 68 | + |
| 69 | + should "create parent directory when it does not exist" do |
| 70 | + ::Dir.mktmpdir do |base_dir| |
| 71 | + dir = ::File.join(base_dir, "nonexistent", "nested") |
| 72 | + TelemetryId.stubs(:config_dir).returns(dir) |
| 73 | + id = TelemetryId.get |
| 74 | + refute_nil id |
| 75 | + assert ::File.exist?(::File.join(dir, "telemetry_id")) |
| 76 | + end |
| 77 | + end |
| 78 | + |
| 79 | + should "return nil when directory cannot be created" do |
| 80 | + TelemetryId.stubs(:config_dir).returns("/nonexistent/readonly/path") |
| 81 | + ::FileUtils.stubs(:mkdir_p).raises(Errno::EACCES, "permission denied") |
| 82 | + assert_nil TelemetryId.get |
| 83 | + end |
| 84 | + |
| 85 | + should "cache the result after first call" do |
| 86 | + ::Dir.mktmpdir do |dir| |
| 87 | + TelemetryId.stubs(:config_dir).returns(dir) |
| 88 | + first_id = TelemetryId.get |
| 89 | + # Modify the file to confirm the second call reads from cache |
| 90 | + ::File.write(::File.join(dir, "telemetry_id"), "different_value") |
| 91 | + second_id = TelemetryId.get |
| 92 | + assert_equal first_id, second_id |
| 93 | + end |
| 94 | + end |
| 95 | + |
| 96 | + should "only call config_dir once even when it returns nil" do |
| 97 | + TelemetryId.stubs(:config_dir).returns(nil) |
| 98 | + TelemetryId.get |
| 99 | + # Second call should use cache; expects config_dir was only called once |
| 100 | + TelemetryId.expects(:config_dir).never |
| 101 | + TelemetryId.get |
| 102 | + end |
| 103 | + end |
| 104 | + |
| 105 | + context ".reset!" do |
| 106 | + should "clear cached state so next call re-evaluates" do |
| 107 | + ::Dir.mktmpdir do |dir| |
| 108 | + TelemetryId.stubs(:config_dir).returns(dir) |
| 109 | + first_id = TelemetryId.get |
| 110 | + refute_nil first_id |
| 111 | + |
| 112 | + TelemetryId.reset! |
| 113 | + ::File.write(::File.join(dir, "telemetry_id"), "new_persisted_id") |
| 114 | + |
| 115 | + second_id = TelemetryId.get |
| 116 | + assert_equal "new_persisted_id", second_id |
| 117 | + end |
| 118 | + end |
| 119 | + end |
| 120 | + |
| 121 | + private def with_env(vars) |
| 122 | + old_values = {} |
| 123 | + vars.each do |key, value| |
| 124 | + old_values[key] = ENV.fetch(key, nil) |
| 125 | + if value.nil? |
| 126 | + ENV.delete(key) |
| 127 | + else |
| 128 | + ENV[key] = value |
| 129 | + end |
| 130 | + end |
| 131 | + yield |
| 132 | + ensure |
| 133 | + old_values.each do |key, value| |
| 134 | + if value.nil? |
| 135 | + ENV.delete(key) |
| 136 | + else |
| 137 | + ENV[key] = value |
| 138 | + end |
| 139 | + end |
| 140 | + end |
| 141 | + end |
| 142 | +end |
0 commit comments