Skip to main content

Posts

Unit testing CLI scripts

I've been building tests for my scripts for some time now; however, I've avoiding testing the interaction with CLI scripts simply because I didn't know how to mock user input and test the script's output. This past week I decided overcome this hurdle and look further into mocking user input. I was surprised by how not difficult it actually was. Of course there is unittest.mock, but that didn't give me the control I wanted when running a script and testing it end to end. After a bit of digging I found that the key to solving this problem was overloads, specifically overloading input, getpass(if you prompt for it), and print. I found an article called Mocking input and output for Python testing on code-maven covering this. I then expanded on it to make it work for my application. I guess it's best to demonstrate and then go back and explain the code. So here's an app, and it's test.py: cli_app.py: from __future__ import print_function from getpass i...
Recent posts

Running additional apps along side of your Django app

In a follow up to last night's post on setting up Django on WHM server, lets say you have a Django app you want to use on the main site, but you have additional code or apps(like a php forum) that you want to also use. After a little digging I beleive I've found the ideal configuration for this. There are 2 parts: Set up your django app's alias on a path other than the root of the site.  In my setup I've put the django app on the /site path. Modify your .htaccess in the root of the web site as follows: # BEGIN Django RewriteEngine On RewriteBase / RewriteRule "^$" "/site/$1" [QSA,PT,L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule "^(.*)$" "/site/$1" [QSA,PT,L] # END Django To break this down: We're applying this to all files from th root up: RewriteBase / Replace an empty path with the pat...

Django on WHM

I spent pretty much all day today trying to peice together how to get Django running on a WHM server. Although I've done it before, I no longer have the configs form the old server and it was running EasyApache 3 which is no loner used. Since I've spent so much time trying to get to this point and wasn't able to find any clear documentation on how to set it up I figured I'd post it here. First we need to install python 3 since that's what Django requires, however as it's not available in the default repositories for CentOS, you'll need to install the EPEL Release repository by running: yum install epel-release Once the epel-release repository is installed, it's time to install python3, it's virtualenv installer and pip as follows: yum install python34 python34-pip python34-virtualenv After you have python 3 installed we need to compile mod_wsgi which is documented here . However we'll be changing 2 things and we need WHM's apache2...