Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There's probably more than one way to do this, but this is working for me right now:</p> <h1>The command</h1> <pre><code>port = `lsof -p #{Process.pid} -ai TCP -as TCP:LISTEN -Fn | grep ^n | cut -c 4- | uniq`.strip </code></pre> <p>Note that you'll have to do this at some point <em>after</em> the app has loaded - i.e., you can't use this in your <code>environment.rb</code> or <code>application.rb</code> file.</p> <h1>The explanation</h1> <p>Basically what this command does is as follows:</p> <ul> <li><code>lsof</code> is the Unix command for LiSt Open Files</li> <li><code>-p #{Process.pid}</code> limits it to the current process (i.e., your test web server instance)</li> <li><code>-ai TCP</code> limits it to "files" of type TCP (i.e., open TCP ports). (Note: the <code>-a</code> is to make it <em>AND</em> the search with the pid one - the default, using just <code>-i</code> would <em>OR</em> the search)</li> <li><code>-as TCP:LISTEN</code> limits to just TCP ports that are being <em>listened</em> on (as opposed to any open port - like your app's connection to Postgres for example)</li> <li><code>-Fn</code> tells it to only output the "name" column, which in this case will be the IP/port that is being listened on</li> </ul> <p>The output of that part by itself will be something like this:</p> <pre><code>p12345 n*:5001 n*:5001 </code></pre> <p>The first line, starting with <code>p</code> is the process ID. There's no way to suppress this. The next 2 lines (not sure why it can output multiples, but we'll take care of it in a minute) are the "name" column (hence <code>n</code>), followed by the IP + port. In our case (and I imagine yours as well, in a test environment), the web server listens on <em>all</em> available local IPs, thus <code>*</code>. Then it tells us that the port is, in this case 5001.</p> <p>Finally, we pipe it through... * <code>grep ^n</code> to eliminate the first line (the process id) * <code>cut</code> to say "cut from columns 4 on" - i.e., remove the <code>n*:</code> to return <em>just</em> the port, and * <code>uniq</code> to just get the one instance</p> <p>(It will also have a trailing newline, thus the <code>strip</code> call.)</p> <h1>The usage</h1> <p>In my case, I'm using this in my Cucumber <code>env.rb</code> thusly, to reconfigure the URL options for ActiveMailer so my email links get generated properly as working links in test:</p> <p>port = <code>lsof -p #{Process.pid} -ai TCP -as TCP:LISTEN -Fn | grep ^n | cut -c 4- | uniq</code>.strip</p> <p>MyApp::Application.configure do config.action_mailer.default_url_options[:host] = "0.0.0.0:#{port}" end</p> <p>No doubt you could do the same thing in a helper/config for Rspec as well. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload