CircleCI - How to build and test in a sub directory? - python

I have an online course platform project. The tree looks like this:
app
-- edxapp
-- edx-platform
-- circle.yml
I want to run the circle.yml in the edx-platform directory. I've followed their documentation here.
First, I created a new circle.yml in the root directory so that the tree looks like this:
circle.yml
app
-- edxapp
-- edx-platform
-- circle.yml
The new circle.yml contains the following:
general:
build_dir: app/edxapp/edx-platform
But, it still didn't work. Then I tried another way. I linked the circle.yml files so that I have one circle.yml in each directory. Each circle.yml just contains the build_dir key with its value pointing to the next sub directory.
Please give me an explanation why this doesn't work. Also, please give me an alternative way to do it.
Note: The project structure has to be the same.

circle.yml needs to be in the root directory of a repository. That file in any other location will not be processed by CircleCI. Anything that you need to do (any commands, etc), needs to be done from that root file.
build-dir changes where the commands from circle.yml are run from, not where the file should be. In essence, whichever directory is set as build-dir, that becomes the working directory for commands in circle.yml. More details in CircleCI Docs.
In summary, have only 1 circle.yml directory, in the root of your repository. If you had a command ls, this would print the directory listing for ~/MY-REPO-NAME. If you set the build-dir to:
general:
build_dir: app/edxapp/edx-platform
then that same ls command will now print the directory listing of ~/MY-REPO-NAME/app/edxapp/edx-platform.
Regards,
Ricardo N Feliciano
Developer Evangelist, CircleCI

Related

Why does Coverage.py ignore files with no coverage?

I first run
nosetests --with-coverage
So I should have a .coverage file with all the default settings.
Within folder_1, I have file_1.py, file_2.py, and file_3.py
When I cd into folder_1 and run
coverage report
It outputs:
It doesn't generate anything for file_3.py! But then when I run:
coverage report file_3.py
it says:
Does it skip files with no coverage in the report? How can I change it so the report shows me the results of every *.py file?
You need to specify a source directory for coverage.py to find files that have never been executed at all. You can use --source=folder_1 on the command line, or [run] source=folder_1 in your .coveragerc file.
I ran into this same scenario yesterday and lost some time trying make Coverage consider the corresponding to this file_3.py. Ned Batchelder's answer is completely correct and helped me but when handling multiple folder_1 folders in the same level in the hierarchy I'd have to set all of them as source and that is not ideal.
The key is this part of the official doc:
If the source option is specified, only code in those locations will be measured. Specifying the source option also enables coverage.py to report on unexecuted files, since it can search the source tree for files that haven’t been measured at all. Only importable files (ones at the root of the tree, or in directories with a __init__.py file) will be considered.
So unexecuted files will only be analysed if you point at them. For this scenario that means two options:
Set your folder directly as source directory running the tests with the flag --source=folder_1 (which is covered on Neds' answer).
If this is a subfolder of a bigger project you can also just set the source folder as the main project folder but then you need to set the directories you want analysed as packages, creating an __init__.py file in them.
For instance if you have:
src/
folder_1/
__init__.py
file_1.py
file_2.py
file_3.py
You can just run with the flag --source=src and folder_1 files will be discoverable as well.
Hope that helps someone in the future.

Mercurial hook precommit get repository path

I'm coding a script to check syntax of repository files.
So, I added to my .hgrc file this line :
precommit = python ~/tools/check_syntax.py
But I want get the path of repository in which I try to commit from check_syntax.py file.
Do you know a way to do that ?
hg root will give you the top of your current working directory. So something like:
[hooks]
precommit = python ~/tools/check_syntax.py $($HG root)
will set the top of your current working directory as first argument of your script.

What do I specify for "Rope project root folder: . " with python files and RopeVim plugin?

I've installed the plugin RopeVim (using Pathogen) and it seems to be working.
Now when I call :RopeGoToDefinition with my vim cursor (in command mode) on a function I'd like to see the definition of...I get:
Rope project root folder: .
displayed in the status line of my vim (fwiw, I'm using MacVim).
What is the proper folder to specify here?
My project folder structure has a root folder, and various subdirs. I can't even tell if I should be specifying a system filepath or a python-style module.
See https://github.com/python-rope/rope/blob/master/docs/overview.rst#ropeproject-folder and the Getting Started section of https://github.com/python-rope/rope/blob/master/README.rst
You can stick it anywhere, but you probably want to use the root directory of your project. Note how the prompt defaults to ., the current directory. If your file is in the root directory, you can just hit Enter to accept that default. Otherwise, try something like ../.. or /path/to/root.

"ImportError: No module named" error doesn't seem to be related to my code

I am trying to complete Exercise 47 of Learn Python The Hard Way, 2nd Edition, but I am receiving this error when I run tests/ex47_tests.py:
File "tests/ex47_tests.py", line 3, in <module>
from ex47.game import Room
ImportError: No module named ex47.game
I thought it was something I was doing wrong within my code because I am very new at this, so I cloned this repo from a GitHub user who seems to have successfully completed the exercise.
Not only are the relevant parts of our code identical, but I receive the same error when I try to run the tests/ex47_tests.py that I cloned from him. So now I am lost and hoping that someone has a solution for me. Any ideas?
fabrizioM's answer should get it to work. Here is a little explanation.
When Python loads a file, it searches on the filesystem. So here, we have the import statement:
from ex47.game import Room
It looks for the file ex47.py on the modules search path (accessible as sys.path in Python code). The modules search path contains some directories based on the installation details of Python, the directories listed in the PYTHONPATH environment variable, and contains the parent directory of the script you’re executing. It doesn't find ex47.py on the path, but it sees there is a directory named ex47 with __init__.py inside of it. It then finds game.py in that folder.
The problem is that your current folder is not on the modules search path. Because ex47_tests.py was run, it has $cwd/tests on the path. You need $cwd on the path.
PYTHONPATH=. python tests/ex47_tests.py
does exactly that. It puts the $cwd on the modules search path so Python can find the source files.
You can also do:
python -m tests.ex47_tests
This will run it as a module instead of a file, while it will use the current directory as path it adds automatically to the the modules search path instead of the directory the file is located inside.
from the repository directory :
PYTHONPATH=. python tests/ex47_tests.py
Make sure there are no other ex47.py files/packages in your path.
The book asks you to copy the 'skeleton' directory and then use it for the game room exercise (#47). The skeleton directory has a "NAME" directory.
So if you copied the skeleton directory and named it ex47, you'll have another ex47 directory inside that.
lpthw > ex47 > ls
bin docs ex47 setup.py tests
So when the book says "Next, create a simple file ex47/game.py where you can put the code to test", you assume that it is the top level ex47. Not correct! That's why the import won't resolve.
I hit the same problem too until I realized that the book calls the outer directory (the one we got by copying 'skeleton') as 'simplegame' evident from this line in the book -
~/projects/simplegame $ nosetests
So all the answers about PYTHONPATH are valid, I just wanted to explain why the import wouldn't work for you!
There is something wrong with your directory structure. Here is my directory structure:
bin docs ex47 setup.py tests
./bin:
./docs:
./ex47:
game.py game.pyc __init__.py __init__.pyc
./tests:
ex47_tests.py ex47_tests.pyc __init__.py __init__.pyc
The project is named ex47 and there is a folder named ex47 within it.
(provided you have named your files according to his plan laid out before game.py file's code in this exercise)
When the author says create a simple file ex47/game.py he means the folder within is where the game.py file should be created and stored.
I too had the same error; being new to python, I followed the book example and after copying the skeleton directory over, renamed any files containing 'NAME' - one existed in the test/ directory and once removed the unable to load error disappeared - check the answer/contents in Microivan directory post above.
So within the project directory only two python files should exist, game.py and ex47_tests.py - hope you got there, on to the next exercise.!!

Python directory/module structure and importing

I'm trying to write a test for my python program, and I've created a /tests directory, where / means the projects root, not system root.
I have all my source files in /myProjectName.
Both directories have a __init__.py file, but project root does not, and both files are empty (do I need a __init__.py in the tests directory ?)
I've tried importing /myProjectName/main.py in /tests/test_main.py, but it doesn't work.
What is the right way to either structure the project directories or import main.py in test_main.py?
I would suggest moving your tests directory to be inside your project directory. Then you can use the answers here to import from the parent directory when you're in tests: Importing modules from parent folder
Otherwise, you can simply set your $PYTHONPATH to point to your project directory when you run tests.
I would like to suggest nose here as it automatically detects the tests and runs them for you
To run the tests, simply do a
$ nosetests
in your project directory
Why not try this line in testmain.py:
import os
os.chdir("/myProjectName/main.py ")
Then execute the script.
Hope it works!

Categories