Skip to content

Commit

Permalink
Fix "No such file or directory @ rb_file_s_rename" on migrating to '~…
Browse files Browse the repository at this point in the history
…/.local/state' (#29)
  • Loading branch information
texpert authored Apr 16, 2024
1 parent 10790d5 commit 7159819
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 13 deletions.
12 changes: 5 additions & 7 deletions lib/localhost/authority.rb
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ def load(path = @root)

certificate_path = File.join(path, "#{@hostname}.crt")
key_path = File.join(path, "#{@hostname}.key")

return false unless File.exist?(certificate_path) and File.exist?(key_path)

certificate = OpenSSL::X509::Certificate.new(File.read(certificate_path))
Expand Down Expand Up @@ -228,12 +228,10 @@ def save(path = @root)
def ensure_authority_path_exists(path = @root)
old_root = File.expand_path("~/.localhost")

if File.directory?(old_root) and not File.directory?(path)
# Migrates the legacy dir ~/.localhost/ to the XDG compliant directory
File.rename(old_root, path)
elsif not File.directory?(path)
FileUtils.makedirs(path, mode: 0700)
end
FileUtils.mkdir_p(path, mode: 0700) unless File.directory?(path)

# Migrates the legacy dir ~/.localhost/ to the XDG compliant directory
FileUtils.mv("#{@old_root}/.", path, force: true) if File.directory?(old_root)
end
end
end
31 changes: 25 additions & 6 deletions test/localhost/authority.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,31 @@
require 'tempfile'

describe Localhost::Authority do
def before
@old_root = File.expand_path("~/.localhost")
@old_root_exists = File.directory?(@old_root)

if @old_root_exists
@tmp_folder = File.expand_path("~/.localhost_test")
FileUtils.mkdir_p(@tmp_folder, mode: 0700)
FileUtils.cp_r("#{@old_root}/.", @tmp_folder)
end
end

def after
if @old_root_exists
FileUtils.mkdir_p(@old_root, mode: 0700)
FileUtils.mv("#{@tmp_folder}/.", @old_root, force: true)
FileUtils.rm_r(@tmp_folder)
end
end

let(:xdg_dir) { File.join(Dir.pwd, "state") }
let(:authority) {
ENV["XDG_STATE_HOME"] = xdg_dir
subject.new
}

with '#certificate' do
it "is not valid for more than 1 year" do
certificate = authority.certificate
Expand All @@ -47,23 +66,23 @@
authority.save(authority.class.path)
expect(File).to be(:exist?, authority.certificate_path)
expect(File).to be(:exist?, authority.key_path)

expect(authority.key_path).to be == File.join(xdg_dir, "localhost.rb", "localhost.key")
expect(authority.certificate_path).to be == File.join(xdg_dir, "localhost.rb", "localhost.crt")
end

it "properly falls back when XDG_STATE_HOME is not set" do
ENV.delete("XDG_STATE_HOME")
authority = subject.new

authority.save(authority.class.path)
expect(File).to be(:exist?, authority.certificate_path)
expect(File).to be(:exist?, authority.key_path)

expect(authority.key_path).to be == File.join(File.expand_path("~/.local/state/"), "localhost.rb", "localhost.key")
expect(authority.certificate_path).to be == File.join(File.expand_path("~/.local/state/"), "localhost.rb", "localhost.crt")
end

with '#store' do
it "can verify certificate" do
expect(authority.store.verify(authority.certificate)).to be == true
Expand Down

0 comments on commit 7159819

Please sign in to comment.