Showing posts with label programming. Show all posts
Showing posts with label programming. Show all posts

Tuesday, March 25, 2014

Vim setup for Chef(Opscode) Cookbooks

I've started programming seriously Chef cookbooks by a while but always felts is something missing ... Well I didn't have

  • jump to definition for any Chef dsl
  • auto completion
  • syntax highlight

Recently I found a solution for this, this is my vim setup(just as easy you can do it in Sublime Text as well) These are the tools in my setup

  • vim
  • vim-chef
  • ripper-tags (by my surprise ctags doesn't work well with ruby files ...)

To setup is as simple as

# vim with pathogen
$ git clone https://github.com/vadv/vim-chef ~/.vim/bundle/vim-chef
$ sudo /opt/chef/embedded/bin/gem install gem-ripper-tags
$ knife cookbook create test_coobook -o .
# create tags - there are better ways to do it - see gem-tags for example
$ ripper-tags -R /opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.10.4 -f tags
$ ctags -R -f tags_project
vim 
:set tags=tags,tags_project 
# done 

Wednesday, November 20, 2013

Javascript testing with Real Browsers


karma run --runner-port 9100
PhantomJS 1.4 (Linux): Executed 1 of 1 SUCCESS (0.397 secs / 0.071 secs)
Chrome 30.0 (Linux): Executed 1 of 1 SUCCESS (0.518 secs / 0.06 secs)
TOTAL: 2 SUCCESS

Wednesday, February 29, 2012

Tricks with nose and python

Nose is a very useful tool for running unittest in python. These are a few tricks you can use. Bellow is my test file - I called it test.py

# dummy case test 

class Test():

    def test_algo(self):
        assert 0 == 0, '0 is not equal to 0'

    def test_failed(self):
        print 'this will fail'
        assert 1 == 0, '0 is not equal to 1'

    def test_fail_inpdb(self):
        # div by 0
        1/0

Now let's see what is this about

  1. first I use assert to check if the results match
  2. based on assert the first function will pass and second will fail
  3. the last function will trigger and Error not a Failure

# running with pdb so any Error not Failure will drop me into python debugger
$ nosetests  --pdb  test.py
.> /home/silviud/PROGS/PYTHON/wal/tests/test.py(14)test_fail_inpdb()
-> 1/0
(Pdb) l
  9             print 'this will fail'
 10             assert 1 == 0, '0 is not equal to 1'
 11     
 12         def test_fail_inpdb(self):
 13             # div by 0
 14  ->         1/0          #### this is the line that triggers the error
[EOF]
(Pdb) c
EF
======================================================================
ERROR: tests.test.Test.test_fail_inpdb
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/silviud/Environments/2.7/lib/python2.7/site-packages/nose/case.py", line 197, in runTest
    self.test(*self.arg)
  File "/home/silviud/PROGS/PYTHON/wal/tests/test.py", line 14, in test_fail_inpdb
    1/0
ZeroDivisionError: integer division or modulo by zero

======================================================================
FAIL: tests.test.Test.test_failed
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/silviud/Environments/2.7/lib/python2.7/site-packages/nose/case.py", line 197, in runTest
    self.test(*self.arg)
  File "/home/silviud/PROGS/PYTHON/wal/tests/test.py", line 10, in test_failed
    assert 1 == 0, '0 is not equal to 1'
AssertionError: 0 is not equal to 1
-------------------- >> begin captured stdout << ---------------------
this will fail

--------------------- >> end captured stdout << ----------------------

----------------------------------------------------------------------
Ran 3 tests in 8.453s

FAILED (errors=1, failures=1)

Now let's pretended that I run this regular and is part of my Continuous Integration server which so happen
to be running Jenkins. How can I integrate the python unittests with it ?!
Simple - nose has many plugins and one of them is xunit.

$ nosetests  --with-xunit test.py
....
$ cat nosetests.xml
<?xml version="1.0" encoding="UTF-8"?><testsuite name="nosetests" tests="3" errors="1" failures="1" skip="0"><testcase classname="tests.test.Test" name="test_algo" time="0.000" /><testcase classname="tests.test.Test" name="test_fail_inpdb" time="0.000"><error type="exceptions.ZeroDivisionError" message="integer division or modulo by zero"><![CDATA[Traceback (most recent call last):
  File "/usr/lib/python2.7/unittest/case.py", line 321, in run
    testMethod()
  File "/home/silviud/Environments/2.7/lib/python2.7/site-packages/nose/case.py", line 197, in runTest
    self.test(*self.arg)
  File "/home/silviud/PROGS/PYTHON/wal/tests/test.py", line 14, in test_fail_inpdb
    1/0
ZeroDivisionError: integer division or modulo by zero
]]></error></testcase><testcase classname="tests.test.Test" name="test_failed" time="0.001"><failure type="exceptions.AssertionError" message="0 is not equal to 1&#10;-------------------- &gt;&gt; begin captured stdout &lt;&lt; ---------------------&#10;this will fail&#10;&#10;--------------------- &gt;&gt; end captured stdout &lt;&lt; ----------------------"><![CDATA[Traceback (most recent call last):
  File "/usr/lib/python2.7/unittest/case.py", line 321, in run
    testMethod()
  File "/home/silviud/Environments/2.7/lib/python2.7/site-packages/nose/case.py", line 197, in runTest
    self.test(*self.arg)
  File "/home/silviud/PROGS/PYTHON/wal/tests/test.py", line 10, in test_failed
    assert 1 == 0, '0 is not equal to 1'
AssertionError: 0 is not equal to 1
-------------------- >> begin captured stdout << ---------------------
this will fail

--------------------- >> end captured stdout << ----------------------
]]></failure></testcase></testsuite>

Just by adding --with-xunit made nose to active the xunit plugin and it generated an xml file into nosetests.xml - this file can be used by Jenkins to take decisions if the build failed or not !

Saturday, July 24, 2010

Django on Google App Engine

In case that you want to run the head (latest version) of django on the google app engine this will help you. The info bellow is for a new project (in case you need information on how to make it work with an
existing project see the README file from the appengine_helper).

First you need to understand what is different between running django on your server where you have a RDBMS like mysql and the datastore that google provides. To do this look into the following link
http://code.google.com/appengine/articles/appengine_helper_for_django.html

After you are done reading do the following:
(i'm using /tmp as an example, you can change it to whatever you want)

cd /tmp/

- download the appengine_helper_for_django (if the link changed because there is a new version replace bellow the names for helper with the new names)

curl -L -o 'appengine_helper_for_django-r105.zip' 'http://google-app-engine-django.googlecode.com/files/appengine_helper_for_django-r105.zip'


- download  the latest version of django (you can download it or use the svn to checkout)

curl -L -o 'Django-1.2.1.tar.gz' 'http://www.djangoproject.com/download/1.2.1/tarball/'


- untar the django

tar xvfz Django-1.2.1.tar.gz


- make a new temporary directory for your application

mkdir myapp_tmp


- extract the django helper 

cd myapp_tmp
unzip ../appengine_helper_for_django-r105.zip


- rename the helper

mv appengine_helper_for_django/ myapp


- copy the django framework to have it available

cp  -r ../Django-1.2.1/django  myapp


- configure the app and run the webserver

cd myapp
ls    (you should have something like this)


HANGES VERSION appengine_django manage.py urls.pyc
COPYING __init__.py django settings.py
KNOWN_ISSUES __init__.pyc index.yaml settings.pyc
README app.yaml main.py urls.py


- edit the app.yaml to reflect your application name etc.


- run the webserver to test

python manage.py runserver
(you should see the output from the app engine starting)

"""

/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/api/datastore_file_stub.py:40: DeprecationWarning: the md5 module is deprecated; use hashlib instead
  import md5
/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/api/memcache/__init__.py:31: DeprecationWarning: the sha module is deprecated; use the hashlib module instead
  import sha
WARNING:root:Could not read datastore data from /var/folders/TI/TIS6v9poFPyMZ3GaFkfkjE+++TQ/-Tmp-/django_vpsup.datastore
WARNING:root:Could not initialize images API; you are likely missing the Python "PIL" module. ImportError: No module named _imaging
INFO:google.appengine.tools.appengine_rpc:Server: appengine.google.com
INFO:root:Checking for updates to the SDK.
INFO:root:The SDK is up to date.
"""

that's it ! you can start using django as you normally do.

(in case you want to change the location of your myapp directory all you have to do is to move the myapp to the new destination - mv /tmp/myapp_tmp/myapp /home/myuser/applications/myapp)









Friday, August 21, 2009

CakePHP - start a project with the console application

To start fast a new project with cakephp 1.2 you will need:

- web server - I use apache 2.2
- php4 or php5 - I use php5
- mysql server - I use mysql 5.0.45
- cakephp - I use cake_1.2.4.8284

After the web and mysql servers are installed install the cakephp - I choosed the path

/var/www/frameworks/cake_1.2.4.8284

Then open a xterm or any shell:

cd /var/www/frameworks/cake_1.2.4.8284/cake/console
cake bake help

Will give you all the info you need to know on how to bootstrap your new project.
I choosed the following:

cake bake project
Then just answer the questions that cake asks:


Welcome to CakePHP v1.2.4.8284 Console
---------------------------------------------------------------
App : app
Path: /var/www/frameworks/cake_1.2.4.8284/app
---------------------------------------------------------------
What is the full path for this app including the app directory name?
Example: /var/www/frameworks/cake_1.2.4.8284/app/myapp
[/var/www/frameworks/cake_1.2.4.8284/app/myapp] > /var/www/frameworks/cake_1.2.4.8284/app/slash0
Bake Project
Skel Directory: /var/www/frameworks/cake_1.2.4.8284/cake/console/libs/templates/skel
Will be copied to: /var/www/frameworks/cake_1.2.4.8284/app/slash0
---------------------------------------------------------------
Look okay? (y/n/q)
[y] > y
Do you want verbose output? (y/n)
[n] > n
---------------------------------------------------------------
Created: slash0 in /var/www/frameworks/cake_1.2.4.8284/app/slash0
---------------------------------------------------------------

Creating file /var/www/frameworks/cake_1.2.4.8284/app/slash0/views/pages/home.ctp
Wrote /var/www/frameworks/cake_1.2.4.8284/app/slash0/views/pages/home.ctp
Welcome page created
Random hash key created for 'Security.salt'
CAKE_CORE_INCLUDE_PATH set to /var/www/frameworks/cake_1.2.4.8284 in webroot/index.php
CAKE_CORE_INCLUDE_PATH set to /var/www/frameworks/cake_1.2.4.8284 in webroot/test.php
Remember to check these value after moving to production server



Change myapp to whatever name you want to have for the app.I put slash0.
At this point the application project is created into the path you specified.

/var/www/frameworks/cake_1.2.4.8284/app/slash0

From now on all you have to do is follow the cakephp book on how to develop :)
See http://book.cakephp.org