Setting up Discourse for testing on a Mac


(Peter N Lewis) #1

[Maybe this belongs on howto but I can’t post there]

My goal was to be able to run the test suite on a Mac. In the end, this took about 6 hours to setup, so I figure I will document the pain here in the hopes that it helps someone. This is for Mac OS X 10.9.3, heaven knows if it will work for any other version.

I assume you don’t want to affect your system more than necessary (especially globally) and I’m using tcsh, so adjust for bash.

Things to do:

Install MacPort The MacPorts Project -- Download & Installation - installing MacPort can take a long time, so be patient.

Clone discourse into ~/unix/discourse

cd ~
mkdir ~/unix
cd ~/unix
git clone git://github.com/discourse/discourse/git
cd discourse

Set gems to be installed for this user only

echo install: --user-install >> ~/.gemrc
echo update: --user-install >> ~/.gemrc

Setup environment variables:

setenv PATH ~/.gem/ruby/2.0.0/bin:/opt/local/lib/postgresql91/bin:$PATH
setenv ARCHFLAGS "-arch x86_64"
setenv RAILS_ENV test

Update MacPorts

sudo port selfupdate
sudo port upgrade outdated

Install bundler

gem install bundle
rehash

Install Xcode.app

and command line tools:

xcode-select --install

Install libraries

sudo port install libxml2 libxslt

Install postgresql and redis

sudo port install postgresql91 postgresql91-server redis

Setup postgresql

rehash
sudo mkdir -p /opt/local/var/db/postgresql91/defaultdb
sudo chown postgres:postgres /opt/local/var/db/postgresql91/defaultdb
sudo su postgres -c ‘/opt/local/lib/postgresql91/bin/initdb -D /opt/local/var/db/postgresql91/defaultdb’

In a separate Terminal window, start postgres

sudo -u postgres /opt/local/lib/postgresql91/bin/postgres -D /opt/local/var/db/postgresql91/defaultdb

Start redis:

sudo port load redis

Setup redis:

rehash
mkdir /tmp/redis
chmod 777 /tmp/redis
redis-cli flushall
redis-cli
127.0.0.1:6379> CONFIG SET dir /tmp/redis
OK
127.0.0.1:6379> CONFIG SET dbfilename temp.rdb
OK
quit
redis-cli flushall

Install the discourse gem dependencies (into ~/unix/bundler)

bundle install --path ~/unix/bundler

This will take a long time, so be patient again.

Create the postgres user (change YOURUSERNAME to your user name

sudo -u postgres /opt/local/lib/postgresql91/bin/createuser YOURUSERNAME
Shall the new role be a superuser? (y/n) y
Shall the new role be allowed to create databases? (y/n) y
Shall the new role be allowed to create more new roles? (y/n) y

The default postgres template may be ASCII and needs to be UTF8, so fix this (see ruby on rails - PG::Error: ERROR: new encoding (UTF8) is incompatible - Stack Overflow) (you can try without this, it will depend on your system settings as to whether the default is UTF8 or ASCII).

psql -U postgres template1
CREATE DATABASE temp WITH TEMPLATE = template0 ENCODING = ‘UNICODE’;
\c temp
DROP DATABASE template1;
CREATE DATABASE template1 WITH TEMPLATE = template0 ENCODING = ‘UNICODE’;
UPDATE pg_database SET datistemplate = TRUE WHERE datname = ‘template1’;
\c template1
DROP DATABASE temp
VACUUM FREEZE;
^D

Create the test database

bundle exec db:drop db:create db:migrate

And now run the test:

bundle exec rake spec

Wait about five minutes for the tests to run.

If you get problems installing nokogiri, you probably have old or outdated versions or architectures of libxml2, libiconv or libxslt, consider trashing any old versions and reinstalling from macports.

If you get problems about missing categories table, you probably have not set the RAILS_ENV to test.

Good luck!


(Arpit Jalan) #2

Hi! There is a detailed howto for doing this:


(Jeff Atwood) #3

@peternlewis sorry we weren’t able to get that howto in front of you earlier.

Is there any way that your guide deviates from the official one, or ways we can improve it?


(Peter N Lewis) #4

Such is life. I suspect I would still have hit problems, since a big chunk of time was getting nokogiri to install, which I suspect was two to various old versions of libxml2/libxslt scattered around my harddisk (installing on the laptop had no problems at all with nokogiri).

I have deleted everything from the laptop and will try the How To.

First step fails from tcsh - even though it is executing the bash command, the actual commandline doesn’t work in tcsh (or sh), but bash is the default, so thats not really a big deal.

It crashed out at Configuring Bundler because I had not set the gem path even though I had set the .gemrc to use user-install. The bigger problem is that the script is not safe to rerun after ay failure.

The part that adds source ~/.bash_profile to .bashrc which does not guard against it already being there (also, that code would be better without the printf line being duplicated).

The brew start commands do not guard against the server already being started (“restart” might be ok, since that would presumably start if they were not started).

Also, it seems to want to reapply the patch to ruby and that fails.

Oh well, perhaps its too much to ask for the script to be restartable, although any errors are going to make it challenging for the user otherwise.

It complains the db:test:prepare is deprecated, and that also appears to drop and recreate it, and that recreate fails as described above if template1 is ASCII. I missed a line in my instructions for fixing it:

UPDATE pg_database SET datistemplate = FALSE WHERE datname = ‘template1’;

Or in full:

psql -U postgres template1
CREATE DATABASE temp WITH TEMPLATE = template0 ENCODING = ‘UNICODE’;
\c temp
UPDATE pg_database SET datistemplate = FALSE WHERE datname = ‘template1’;
DROP DATABASE template1;
CREATE DATABASE template1 WITH TEMPLATE = template0 ENCODING = ‘UNICODE’;
UPDATE pg_database SET datistemplate = TRUE WHERE datname = ‘template1’;
\c template1
DROP DATABASE temp
VACUUM FREEZE;
^D

And then it generated 20 spec failures, but I’ve got no idea why (I get no ideas on my desktop with the install as above).

rspec ./spec/models/post_spec.rb:711 # Post sort_order regular topic defaults to created order
rspec ./spec/models/post_spec.rb:696 # Post summary returns the OP and posts above the threshold in summary mode
rspec ./spec/components/post_creator_spec.rb:292 # PostCreator existing topic success create correctly
rspec ./spec/models/topic_spec.rb:926 # Topic scopes #by_most_recently_created returns topics ordered by created_at desc, id desc
rspec ./spec/models/topic_user_spec.rb:243 # TopicUser can scope by tracking
rspec ./spec/components/search_spec.rb:95 # Search escapes non alphanumeric characters
rspec ./spec/components/search_spec.rb:139 # Search topics search within topic displays multiple results within a topic
rspec ./spec/models/report_spec.rb:66 # Report private messages topic report should not include private messages
rspec ./spec/models/report_spec.rb:74 # Report private messages post report should not include private messages
rspec ./spec/models/report_spec.rb:35 # Report post report no posts returns an empty report
rspec ./spec/models/report_spec.rb:55 # Report post report with posts returns correct data
rspec ./spec/models/report_spec.rb:35 # Report topic report no topics returns an empty report
rspec ./spec/models/report_spec.rb:55 # Report topic report with topics returns correct data
rspec ./spec/components/cooked_post_processor_spec.rb:107 # CookedPostProcessor.post_process_images topic image adds a topic image if there’s one in the post
rspec ./spec/components/suggested_topics_builder_spec.rb:76 # SuggestedTopicsBuilder adding results adding topics added the result correctly
rspec ./spec/components/suggested_topics_builder_spec.rb:91 # SuggestedTopicsBuilder adding results adding topics that are not open adds archived and closed, but not invisible topics
rspec ./spec/components/suggested_topics_builder_spec.rb:101 # SuggestedTopicsBuilder adding results category definition topics doesn’t add a category definition topic
rspec ./spec/models/top_topic_spec.rb:22 # TopTopic refresh! after calculating should have top topics
rspec ./spec/components/topic_query_spec.rb:157 # TopicQuery a bunch of topics list_latest sort_order returns the topics in correct order
rspec ./spec/components/topic_query_spec.rb:14 # TopicQuery secure category filters categories out correctly

I expect that indicates something has not been installed properly, but I don’t know what.


(Arpit Jalan) #5

Yes, that is an issue! I’ll modify the script to add checks, ASAP!


(James Cook) #6

I’m also getting the same 20 spec failures after setting up local dev environment using the beginners guide.

I didn’t use the setup script as I had everything already set up on my machine but I’m sure that’s not the problem. Everything seems set up correctly.

I’ve dropped my databases numerous times and re migrated and seeded in case there were any migration issues. I’ve also flushed my redis server but I’m still getting the 20 failures.

Did you ever solve this?


(James Cook) #8

Ok, I figured out what the problem was. Somehow I had seeded data into my test database, no idea how. It would appear that you probably did the same thing as you were getting the same failures.

RAILS_ENV=test bundle exec rake db:reset

The above command should fix the problem.

However, I’m still getting one failure. The ‘finds chinese topic based on title’ in the search_spec is still failing. The search term doesn’t find the topic. I will look into this more another time as I’m tired and about to sleep! :smile: