Using Kazam for screencasting

So I needed to record my screen to show our product owner a certain feature suggestion I had. It turned out that in Linux (debian) Kazam is a pretty good screen recording software, if a bit tricky to use.

First, install Kazam (sudo apt-get install kazam), then start it up and you get to choose whether to take a screen shot or record a screencast. In either cases you can select either a screen, a window or an area to record.
Recording the video is simple enough, just click capture. This hides Kazam and to stop recording you have to press the key combination SUPER + CONTROL + F (where SUPER seems to be the windows-key). This will stop the recording and let you either save or discard it.

This is where it gets tricky, though. The recording is saved as an enormous .avi-file, which is less than optimal if you just want to send it to someone. So, in order to convert it I used a tool called avconv (apt-get), and to convert my avi to a mp4 file I ran this command:

avconv -i Screencast\ 2018-05-15\ 13:45:35.avi -c:a copy my-output-file.mp4

Screencast\ 2018-05-15\ 13:45:35 is a default naming convention that Kazam uses, so just change this to whatever filename you got.

JavaScript: Parsing dates with the destructuring assignment syntax

As I was scraping some stuff in a node module I got dates that were either full dates with time (“2017-01-01 12:00”) or just dates without time (“2017-01-01”) but I wanted to do some checking with months and days before sending back a proper Date-object, so I had to parse it out. Using the new ES6 destructuring assignment syntax it got really good looking:

/**
 * Converts a standard format date string, such as "2017-01-03 12:30" or "2017-05-06"
 */
module.exports = (dateRaw) => {
    if(dateRaw === undefined) {
        throw new Error('Invalid date provided!');
    }

    // Returns ['2017-01-03', '12:30']
    const [date, time] = dateRaw.trim().split(' '); 

    // Returns ['2017', '01', '03']
    const [year, month, day] = date.split('-'); 

    // Returns either the time, or, it it's missing, 0 hours and 0 minutes
    const [hours, minutes] = (time !== undefined) ? time.split(':') : [0, 0]; 

    // Do stuff...

    // To get the timezone correct, use Date.UTC
    return new Date(Date.UTC(year, month - 1, day, hours, minutes));
};

Just thought I’d share, because this used to get slightly ugly in ES5…

Windows 10: Choose an operating system-problem

As I was moving my Windows 10 installation from an old borken HDD to a new, fresh SSD I ended up just using Microsofts super-easy Media Creation Tool to create a bootable USB stick. I booted it up and created a new installation on my new SSD after formatting it. All of this went great and I was happy as a hamster.

Then, when I rebooted, I got this horrible screen:

Horrid horrid screen
Horrid horrid screen

I had no idea why this happened and I got it on every boot, so I did some frantic googling and I found in various suspicious forums that the solution was to do this:

1. Open the windows run dialog (Win + R)

2. Enter ‘msconfig’ and click ‘Ok’run_msconfig3. Go to the boot tab (second tab) and remove all other Windows references than the one marked as default

In this screenshot I have only one Windows 10 entry, but I had two when this problem first occurred.
In this screenshot I have only one Windows 10 entry, but I had two when this problem first occurred.

If you save that and reboot (after you have removed the other Windows 10 entries your boot section should look like mine in the above image, minus the enormous red arrow).

Finished, and the problem is gone!

Git: Rewriting commit messages through a rebase

So I accidentally failed in entering a correctly spelled commit message and pushed it. Someone saw it and I felt I wanted to redo it. If it just the latest commit then no problem, you just do an amend, but I had committed once more afterwards. And pushed.
Since I was still in my own branch the solution was to do an interactive rebase and then use reword. So instead of using the pick command I use reword . I start by executing the rebase command with the interactive flag (-i):

git rebase -i master

Then you will see the interactive feature where you can edit your commits. This may be what it looks like:

pick 0db9efc MAF-105: A fantatsic fix!

# Rebase 552b628..3bc00d5 onto 552b628
#
# Commands:
#  p, pick = use commit
#  r, reword = use commit, but edit the commit message
#  e, edit = use commit, but stop for amending
#  s, squash = use commit, but meld into previous commit
#  f, fixup = like "squash", but discard this commit's log message
#  x, exec = run command (the rest of the line) using shell
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out

Now change the command pick to reword and alter the text. Then you just continue your rebase and your commit comment is changed, like this:

reword 0db9efc MAF-105: A fantastic fix! It's so good!

# Rebase 552b628..3bc00d5 onto 552b628
#
# Commands:
#  p, pick = use commit
#  r, reword = use commit, but edit the commit message
#  e, edit = use commit, but stop for amending
#  s, squash = use commit, but meld into previous commit
#  f, fixup = like "squash", but discard this commit's log message
#  x, exec = run command (the rest of the line) using shell
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out

Let the rebase finish and you’re done!

SQL SERVER: Checking all tables row count and size

This is one of those posts where I just blurt out neat stuff I just learned! Today I talked to a man who knew a lot more about databases than I do (especially SQL Server) and one of the neat things he taught me were the SQL Server stored procedures sp_msForEachTable and sp_spaceused. This enabled me to put together a very wonderful little function to loop over all tables (using sp_msForEachTable) and fetch information about how much space they use (with sp_spaceused).

I created a function that puts it all in a temporary table because otherwise you just got a whole bunch of individual outputs which may be both hard to read and difficult to export:

CREATE TABLE #tableInformation
(
	name VARCHAR(255),
	rows INT,
	reserved varchar(255),
	data varchar(255),
	index_size varchar(255),
	unused varchar(255)
)

INSERT #tableInformation
	EXEC sp_msForEachTable 
		'exec sp_spaceused ''?'''
		
SELECT name, rows, reserved, data, index_size, unused
FROM #tableInformation
ORDER BY name

DROP TABLE #tableInformation

The output can look like this:

The result table
The result table

EPiServer: Unit test classes with Injected properties

This will be a very short entry but I thought it’d be useful to write it down. When you work with EPiServer you may end up having to use property injection. It is very simple, you add a property of type Injected<T>, where T is the interface of whatever you want injected, and the implementation of your interface (according to your DI-settings) will be injected in to this property.
Even though I’m not a huge fan of this solution it seems to be pretty widely used and if you use it, you want to unit test code that uses it.

To test classes with injected properties you want to create your own ServiceLocator which returns a mock of whatever is being used in the code you want to test. I’ve compiled a small scenario where a class of mine, TestClass, has a property of type Injected<IContentLoader> which a method, StuffDoer, uses. I want to mock the injected property to see if it’s being called, here is the code:

public class TestClass
{
    private Injected<IContentLoader> _contentLoader;

    public void StuffDoer(SiteDefinition definition)
    {
        var stuff = _contentLoader
            .Service
            .Get<PageData>(definition.StartPage);

        //And a whole lot of other stuff...
    }
}

[TestFixture]
public class StuffDoesTests
{
    private Mock<IContentLoader> _mockContentLoader;
    private TestClass _testClass;

    [SetUp]
    public void CreateRequirements()
    {
        //Mock the content loader
        _mockContentLoader = new Mock<IContentLoader>();

        _testClass = new TestClass();
    }
    [Test]
    public void IsGet_Called()
    {
        using (ShimsContext.Create())
        {
            //Mock the service locator
            var serviceLocator = new Mock<IServiceLocator>();
            serviceLocator
                .Setup(x => x.GetInstance<IContentLoader>())
                .Returns(_mockContentLoader.Object);

            //Set your mocked service locator to be used
            ServiceLocator.SetLocator(serviceLocator.Object);

            //Call your method
            _testClass.StuffDoer(new SiteDefinition());

            //Verify that the Get<T> method was called!
            _mockContentLoader
                .Verify(x => x.Get<PageData>(It.IsAny<ContentReference>()), Times.Exactly(1));
        }
    }
}