From d744eb023d067b7a266cb4df8b1415f49f3f7a9a Mon Sep 17 00:00:00 2001 From: Eric Budd Date: Tue, 20 Dec 2022 22:13:32 -0600 Subject: [PATCH] Fix bug where "hostname" portion of author is missing or garbled --- CHANGELOG | 3 +++ README.md | 18 ++++++++++++++++-- TODO.md | 2 ++ iris.rb | 23 ++++++++++++++++++----- tests/iris_test.rb | 44 ++++++++++++++++++++++++++++++++++++++------ 5 files changed, 77 insertions(+), 13 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 2b52791..4d57ca8 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,5 +1,8 @@ # Changelog +## 1.1.2 +* Fix bug where "hostname" portion of author is missing or garbled + ## 1.1.1 * Make discarded message response more prominent * Try nano if /usr/bin/vim is not set diff --git a/README.md b/README.md index c3f9b49..5254df0 100644 --- a/README.md +++ b/README.md @@ -596,7 +596,7 @@ Iris must: * Deleted or edited messages should leave flags or placeholders for other users to know that other content was there before. * The Iris client should expect that any message file could be missing, altered, or corrupted, and should handle those cases gracefully. * Be portable - * All Iris files should be human-readable (and -editable, in a pinch) + * All Iris data files should be human-readable (and -editable, in a pinch) * The use of the official Iris client should be optional for a user to manage his or her messages. A text editor should suffice. * Other clients which follow the Iris file format should work seamlessly with the official Iris client. * Be secure @@ -605,7 +605,7 @@ Iris must: * Be a teacher * Code should be clean, well-organized, and readable. * Be limited in scope - * The source code should not exceed 1,000 LOC + * The source code should not exceed 1,000 SLOC ## Tests @@ -622,6 +622,20 @@ To run the tests: ruby tests/iris_test.rb ``` +```bash +Run options: --seed 11507 + +# Running: + +.........................SSS.......SS.......S.....SSSSSSS....SSSSS + +Finished in 0.107785s, 612.3294 runs/s, 677.2734 assertions/s. + +66 runs, 73 assertions, 0 failures, 0 errors, 18 skips + +You have skipped tests. Run with --verbose for details. +``` + ## Technical Bits * [Dependencies](#dependencies) diff --git a/TODO.md b/TODO.md index 0706366..c5770fd 100644 --- a/TODO.md +++ b/TODO.md @@ -10,6 +10,8 @@ * Flesh out technical sections ### Bugs +* Terrible slowdown when refreshing topics + * Performance is fine when no new topics show up * Is `Time.now.utc.iso8601` working as expected? * Fix bug when people are posting from different time zones * Fix message ordering when editing/deleting multiple messages diff --git a/iris.rb b/iris.rb index 86ba58b..37344d8 100755 --- a/iris.rb +++ b/iris.rb @@ -27,13 +27,26 @@ class Config HISTORY_FILE = "#{ENV['HOME']}/.iris.history" IRIS_SCRIPT = __FILE__ - USER = ENV['USER'] || ENV['LOGNAME'] || ENV['USERNAME'] - HOSTNAME = `hostname -d`.chomp - AUTHOR = "#{USER}@#{HOSTNAME}" ENV_EDITOR = ENV['EDITOR'].presence || `which nano`.chomp.presence @@debug_mode = false + def self.hostname + return @hostname if @hostname + + hostname = `hostname`.chomp + hostname = 'localhost' if hostname.empty? + + return @hostname = hostname if hostname == 'localhost' + + @hostname = hostname.split('.')[-2..-1].compact.join('.') + end + + def self.author + user = ENV['USER'] || ENV['LOGNAME'] || ENV['USERNAME'] + @author ||= "#{user}@#{self.hostname}" + end + def self.find_files (`ls /home/**/.iris.messages`).split("\n") end @@ -343,7 +356,7 @@ end class Message attr_reader :timestamp, :edit_hash, :author, :parent, :message, :errors, :is_deleted - def initialize(message, parent = nil, author = Config::AUTHOR, edit_hash = nil, timestamp = Time.now.utc.iso8601, is_deleted = nil) + def initialize(message, parent = nil, author = Config.author, edit_hash = nil, timestamp = Time.now.utc.iso8601, is_deleted = nil) @message = message @parent = parent @author = author @@ -890,7 +903,7 @@ class Interface end def prompt - "#{Config::AUTHOR}~> " + "#{Config.author}~> " end def initialize(args) diff --git a/tests/iris_test.rb b/tests/iris_test.rb index 34118d8..82c9ad8 100644 --- a/tests/iris_test.rb +++ b/tests/iris_test.rb @@ -24,16 +24,48 @@ describe Config do _(Config::HISTORY_FILE).must_match /\/\.iris\.history$/ end - it 'has the username' do - _(Config::USER).must_equal 'jerryberry' - end + describe '.hostname' do + before do + Config.instance_variable_set(:@hostname, nil) + end - it 'has a hostname' do - _(Config::HOSTNAME).wont_be_nil + it 'has a hostname' do + _(Config.hostname).wont_be_nil + end + + it 'correctly interprets an empty string' do + Config.expects(:`).with('hostname').returns('') + _(Config.hostname).must_equal 'localhost' + end + + it 'correctly interprets localhost' do + Config.instance_variable_set(:@hostname, nil) + Config.expects(:`).with('hostname').returns('localhost') + _(Config.hostname).must_equal 'localhost' + end + + it 'correctly interprets a subdomain' do + Config.instance_variable_set(:@hostname, nil) + Config.expects(:`).with('hostname').returns('example.com') + _(Config.hostname).must_equal 'example.com' + end + + it 'correctly interprets a subsubdomain' do + Config.instance_variable_set(:@hostname, nil) + Config.expects(:`).with('hostname').returns('foo.example.com') + _(Config.hostname).must_equal 'example.com' + end + + it 'correctly interprets an arbitrary number of subdomains' do + Config.instance_variable_set(:@hostname, nil) + Config.expects(:`).with('hostname').returns('foo.bar.baz.quux.example.com') + _(Config.hostname).must_equal 'example.com' + end end it 'has the author' do - _(Config::AUTHOR).must_equal "#{Config::USER}@#{Config::HOSTNAME}" + user = 'jerryberry' + _(Config.author).must_equal "#{user}@#{Config.hostname}" end it 'has the $EDITOR environment variable' do