Wednesday, August 10, 2016

Pytest-HTML release 1.10.0: Dealing with CSP

The main topic of the version 1.10.0 of Pytest-HTML was dealing with Content Security Policy (CSP), since the HTML reports started to look terrible on sites (such as Jenkins) where CSP was active. Before the report was something like that:
As you may see, the table report was almost illegible.

To handle with that, I had to solve many issues. I'll tell from the start.

Handling with CSS

The first thing that was breaking the CSP was that Pytest-HTML only had inline CSS, because when the plugin was built, we thought that could be a good idea to have all in a single document, so it could be exported more easily. The problem is that CSP doesn't allow inline CSS, so we had to create a new file to CSS and export it from the plugin, but for people who desire to have all contained in a single file, there is a new option to use with pytest-html: --self-contained-html, when that is active, pytest-html doesn't create any other file other than the HTML file.

Handling with images, text and JSON

The images had a similar problem of CSS, to be in a single file, they were compressed to base64 strings. The problem with that is that a CSP page doesn't accept to render a base64 string as an image. So the solution was similar to the one applied to CSS, there is a  code which creates images on an external file, and there is an option to still creates the images alongside the HTML when it is necessary to easily share that file. There was not problems related with CSP, JSON and text, but there were an open issue suggesting that we should create then in separated files as well, so we did that.

Initial sort order of the Results

Prior, the sort was all made by JS, so when JS was disabled (CSP disallow JS), we lost the ability of sorting the table (there is a default order that results should appear by type,  which is Error, Failed, Rerun, Skipped, Passed), this was also a very cost operation to use with JS when there was a big amount of tests, many times the browser used to stop working when this script was loaded. So the idea was to order that using the python plugin. Since ordering is a very cost operation, the better idea is doing an ordered insertion (every time a row was inserted in the table, it should be inserted on its right position), and for doing that, I created an object that represents the TestResult, and before inserting each test result on the table, I compared it to the others by its outcome. Now, even more, the plugin is object oriented, we have an object that represents the report, other one the TestResult and other one the Outcomes of the test set.

Minor issues: Rerun outcome and filter checkboxes

Since last release, we started to have two new things: support to rerun-failure plugin and the ability to filter the results by its outcome. The problem is that the rerun should not always appear, only when the rerun-failures is enabled, and the filter function is totally dependent on Java Script. Thanks to OOP, doing those changes were quick, I just had to add a condition before creating the rerun object, and change one line of code where the code of the checkboxes were being created inside the rerun  class.

Contribution of external public: collapse and show logs

One enhancement that was asked a little time ago was a button to hide logs, and another one to show those logs, so there was an external contributor named Leitzler who did that.

That is it! And see the result of the new reports of the HTML plugin right here:

Sunday, August 7, 2016

Lessons from Outreachy

It is almost unbelievable that the internship is almost finished! There is only two weeks to go! Seems that it started yesterday! Many things have happened since the beginning and on this post I'll comment my impressions about the program in general.

Now I am almost finished with my main project, which was making the reports look good even with Content Security Police on, you can read details about this project was thought on my mentor's blog. Hopefully, this week, we are going to release the version 1.10.0 of Pytest-HTML which will include my main summer project. When it is ready, I'll write a post about it. Now I am working on an issue that I suggested: creating unit tests to Java Script code, maybe I'll let this issue to our work week in Mountain View - California (yes, I am going into one more trip because of Outreachy! If you like traveling, you should definitely join Mozilla on Outreachy haha :P), and work on minor issues for this week.

During the internship I could learn many things, starting from Python: I just had one semester of Python more than one year ago, and never worked on big projects with it (just a small calculator or something like that)! I only had previous experience in Java, I learned many things, from creating constructors to classes with optional parameters to what was Flake8 (and I thank my mentor, Dave, to be so patient with some of my errors and to help me). It was so good to see that I could learn a new programing language and work on a bigger project without much effort. It was very good to be out of my comfort zone, I feel that I can face any technology change and adapt to it.

Another way to get out of my comfort zone in this project, was that it was my first time working only in English with mostly native English speakers, and when I thought about applying to this program, I was a little afraid that I would not be able to communicate well and my first thought was to apply to a project mentored by a Brazilian, but fortunately he had too much applicants and told me to find another project: talking like that, you may think this guy is rude or something like that, but he is lovely (I meet him in London), it was just because I felt good on this project, and I could assert that my English is not that bad: for sure, sometimes I misunderstand something, or I do some spell/grammar error, but I just can ask my mentor to repeat when I don't understand and write again when I do that (and not just because English is not my native language, because it happens also when I try to communicate in Portuguese). It was good to see, that yes, I can work in English.

Some things that I didn't learn and I am glad because of that were some concepts. I was glad that I already listened about all the data structures and algorithms that I faced during the project (okay, there were not so many, just simple ones like Hash, dictionaries and lists), and OOP concepts were not new at all! I am glad that I could transform the plugin on a more Object-Oriented plugin, because when I got into it, it was almost only functions, and now it has some Objects such as "Outcome" (which represents the outcome of the tests) and TestResult. I think because of that, it was easier to make some changes, such as giving support to Rerun (it was just create a new Outcome object) and just showing Rerun when actually the plugin is installed (it was just a if statement before creating this new object).

Thought, to be a better Mozilla worker, I still have to learn and adapt to many things! It was my first time working remotely with people from many different time zones! I still get confused when there is a team meeting: "Is it going to happen 9 A.M.? No! 9 A.M. California time, I think it is about 6 P.M.? No it is my mentor's time, here it is around 12 AM or PM?" :P. I still have to get more used to use IRC, for some reason, I feel totally weird when I write on one channel: I think, "am I right to post this here?", "Does anyone will be annoyed with that?". I think also, that I should have a dedicated work place and better internet connection, because I mostly work from my room, and I don't like it very much, and having to work from home I realized that my home's connection was not that good (because before I almost always stayed full time at university and used internet there).

Those are the lessons that I learned from Outreachy until now, I hope you liked my report.