all files / sync/gpii/node_modules/packagekit/nodepackagekit/ nodepackagekit_test.js

91.07% Statements 102/112
62.07% Branches 18/29
100% Functions 24/24
91.07% Lines 102/112
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278                                                            121676×                                                                                                                                                                                                                                                                                                    
/*!
GPII Node.js PackageKit Bridge
 
Copyright 2012 Steven Githens
Copyright 2013, 2014 Inclusive Design Research Centre, OCAD University
Copyright 2013, 2014 Emergya
 
Licensed under the New BSD license. You may not use this file except in
compliance with this License.
 
You may obtain a copy of the License at
https://github.com/gpii/universal/LICENSE.txt
*/
 
"use strict";
 
var fluid = require("universal"),
    jqUnit = fluid.require("node-jqunit"),
    gpii = fluid.registerNamespace("gpii"),
    packagekit = require("./build/Release/nodepackagekit.node");
 
fluid.registerNamespace("gpii.tests.packageKit");
 
gpii.tests.packageKit.isInstalled = function (pkg) {
    return (!!pkg && (pkg.data.indexOf("installed") !== -1));
};
 
gpii.tests.packageKit.findGlib2 = function (aPkg) {
    return aPkg.name === "glib2";
};
 
gpii.tests.packageKit.findPackageByName = function (name, pkgArray) {
    return fluid.find(pkgArray, function (aPkg) {
        Eif (aPkg.name === name) {
            return aPkg;
        }
    }, null);
};
 
// Return the package that matches name, version, and architecture.
gpii.tests.packageKit.getMatchingPackage = function (pkg, pkgList) {
    return fluid.find(pkgList, function (aPkg) {
        if (aPkg.name === pkg.name &&
            aPkg.version === pkg.version &&
            aPkg.arch === pkg.arch) {
            return aPkg;
        }
    }, null);
};
 
gpii.tests.packageKit.initTuxguitar = function () {
    var tuxguitar = {
        pkgList: [],
        pkg: null,
        inInstalledList: false,
        inAvailableList: false
    };
    var pkgs = packagekit.searchPackage("tuxguitar");
    tuxguitar.pkgList = pkgs;
    tuxguitar.pkg = gpii.tests.packageKit.findPackageByName("tuxguitar", pkgs);
    return tuxguitar;
};
 
gpii.tests.packageKit.isTuxguitarInList = function (tuxguitar, pkgList) {
    var matchPkg = gpii.tests.packageKit.getMatchingPackage(tuxguitar.pkg, pkgList);
    return (matchPkg !== null);
};
 
// GPII-1081: compensate for change in PackageKit between v0.8.17 and
// v1.0.3.  In the later version, even when installed, the same package
// appears in the list of available packages.  In v0.8.17 it did not.
//
gpii.tests.packageKit.availableNotInstalled = function (tuxguitar, availablePkgs) {
    var isAvailable = gpii.tests.packageKit.isTuxguitarInList(tuxguitar, availablePkgs);
    Eif (tuxguitar.inInstalledList && isAvailable) {
        var installedMatchesAvailablePkg =
        gpii.tests.packageKit.getMatchingPackage(tuxguitar.pkg, availablePkgs);
 
        Eif (installedMatchesAvailablePkg !== null) {
            tuxguitar.inAvailableList = false;
        }
        else {
            tuxguitar.inAvailableList = true;
        }
    }
    else {
        tuxguitar.inAvailableList = isAvailable;
    }
};
 
gpii.tests.packageKit.initSearchTest = function () {
    var tuxguitar = gpii.tests.packageKit.initTuxguitar();
    var installedPkgs = packagekit.searchPackage("tuxguitar", "installed");
    tuxguitar.inInstalledList =
    gpii.tests.packageKit.isTuxguitarInList(tuxguitar, installedPkgs);
 
    var availablePkgs = packagekit.searchPackage("tuxguitar", "~installed");
    gpii.tests.packageKit.availableNotInstalled(tuxguitar, availablePkgs);
 
    return tuxguitar;
};
 
gpii.tests.packageKit.initGetTest = function () {
    var tuxguitar = gpii.tests.packageKit.initTuxguitar();
    var installedPkgs = packagekit.getPackages("installed");
    tuxguitar.inInstalledList =
    gpii.tests.packageKit.isTuxguitarInList(tuxguitar, installedPkgs);
 
    var availablePkgs = packagekit.getPackages("~installed");
    gpii.tests.packageKit.availableNotInstalled(tuxguitar, availablePkgs);
 
    return tuxguitar;
};
 
gpii.tests.packageKit.runInstalledVsAvailableTests = function (tuxguitar, msg) {
    // Depending on whether tuxguitar is installed on not, check that it
    // appears  correctly in the installed or available lists.
    Eif (gpii.tests.packageKit.isInstalled(tuxguitar.pkg)) {
        jqUnit.assertTrue(msg + " 'tuxguitar' in installed packages list",
                          tuxguitar.inInstalledList);
        jqUnit.assertFalse(msg + " 'tuxguitar' not in available packages " +
                           "list", tuxguitar.inAvailableList);
    }
    else {
        jqUnit.assertFalse(msg + " 'tuxguitar' not in installed packages " +
                           "list", tuxguitar.inInstalledList);
        jqUnit.assertTrue(msg + " 'tuxguitar' is in available packages list",
                          tuxguitar.inAvailableList);
    }
};
 
gpii.tests.packageKit.searchForMatch = function (tuxguitar, searchFilter) {
    var pkgs = packagekit.searchPackage("tuxguitar", searchFilter);
    return gpii.tests.packageKit.getMatchingPackage(tuxguitar.pkg, pkgs);
};
 
gpii.tests.packageKit.testRemovePackage = function (tuxguitar, matchPkg) {
    var id = (matchPkg !== null ? matchPkg.id : tuxguitar.pkg.id);
 
    // GPII-880:  The 'remove' action requires an administrator password.
    // Packagekit invokes PolKit authentication, putting up a dialog to
    // capture aforesaid password.  This needs to be automated.
    packagekit.performAction("remove", id);
    matchPkg = gpii.tests.packageKit.searchForMatch(tuxguitar, "~installed");
    jqUnit.assertNotNull("Remove tuxguitar package", matchPkg);
    return matchPkg;
};
 
gpii.tests.packageKit.testInstallPackage = function (tuxguitar, matchPkg) {
    var id = (matchPkg !== null ? matchPkg.id : tuxguitar.pkg.id);
    packagekit.performAction("install", id);
    matchPkg = gpii.tests.packageKit.searchForMatch(tuxguitar, "installed");
    jqUnit.assertNotNull("Install tuxguitar package", matchPkg);
    return matchPkg;
};
 
gpii.tests.packageKit.testPerformActionFailure = function () {
    var errMsg = "No Error Message";
    try {
        packagekit.performAction("foo", "bar");
    }
    catch (error) {
        errMsg = error.message;
    }
    return errMsg;
};
 
jqUnit.module("PackageKit Bridge node add-on module");
 
jqUnit.test(
    "Test searchPackage() of 'glib2' with implicit 'none' filter, meaning " +
    "installed or available.", function () {
 
    // Packagekit-glib -- the code this add-on invokes -- itself depends on
    // glib2.  It must be installed, if this is running.
    var pkgs = packagekit.searchPackage("glib2");
    var found = pkgs.some(gpii.tests.packageKit.findGlib2);
    jqUnit.assertTrue("Search 'glib2', implicit 'none' filter", found);
});
 
jqUnit.test("Test searchPackage() 'glib2' with explicit 'none' filter.", function () {
    var pkgs = packagekit.searchPackage("glib2", "none");
    var found = pkgs.some(gpii.tests.packageKit.findGlib2);
    jqUnit.assertTrue("Search 'glib2', explicit 'none' filter", found);
});
 
jqUnit.test("Test searchFiles() for '/usr/bin/ls'.", function () {
 
    // The searchFiles() function expects the full path name where the package
    // would be installed, even it if is not installed.  Check using the
    // common utility 'ls'.
    var pkgs = packagekit.searchFiles("/usr/bin/ls");
    jqUnit.assertNotEquals(
        "Search file '/usr/bin/ls', implicit 'none' filter", 0, pkgs.length
    );
    jqUnit.assertTrue("'ls' is installed", gpii.tests.packageKit.isInstalled(pkgs[0]));
});
 
jqUnit.test("Test searchPackage() with 'tuxguitar' comparing installed " +
             "vs. available filters.", function () {
 
    // Test the "installed" vs. "~installed" filters with regards to
    // 'tuxguitar', and the array of packages returned by searchPackage()
    // using the filters.  Use the tuxguitar package, whether installed or
    // not, to test against the two arrays.
    var tuxguitar = gpii.tests.packageKit.initSearchTest();
    gpii.tests.packageKit.runInstalledVsAvailableTests(tuxguitar, "Search");
});
 
jqUnit.test("Test getPackages() with tuxguitar comparing installed vs. " +
            "available filters.", function () {
    var tuxguitar = gpii.tests.packageKit.initGetTest();
    gpii.tests.packageKit.runInstalledVsAvailableTests(tuxguitar, "Get");
});
 
jqUnit.test("Test performAction(): 'install' and 'remove' tuxguitar." +
            "  The order depends on whether tuxguitar is currently " +
            "installed.", function () {
 
    // GPII-880:  The 'remove' test requires an administrator password.
    // Packagekit invokes PolKit authentication, putting up a dialog to
    // capture aforesaid password.  This needs to be automated.
 
    // Using the tuxguitar package:  If it's installed, test removing it and
    // then (re)install it ...
    var matchPkg;
    var tuxguitar = gpii.tests.packageKit.initTuxguitar();
    Eif (gpii.tests.packageKit.isInstalled(tuxguitar.pkg)) {
        matchPkg = gpii.tests.packageKit.testRemovePackage(tuxguitar, null);
        gpii.tests.packageKit.testInstallPackage(tuxguitar, matchPkg);
    }
    // ...if it isn't installed, test installing it and then removing it.
    else {
        matchPkg = gpii.tests.packageKit.testInstallPackage(tuxguitar, null);
        gpii.tests.packageKit.testRemovePackage(tuxguitar, matchPkg);
    }
});
 
jqUnit.test("Test performAction() with invalid argments", function () {
    var errorMessage = gpii.tests.packageKit.testPerformActionFailure();
    jqUnit.assertEquals(
        "Result of ThrowError():",
        "You have to provide the action to be performed, either " +
        "'install', 'update' or 'remove'",
        errorMessage
    );
});
 
jqUnit.test("Test updatePackage(): with 'emacspeak'", function () {
 
    // The package ids of an old and newer version of 'emacspeak'.
    // TODO:  JS:  While the following check against two version of
    // 'emacspeak' works on Fedora-20, it may not work on all distros; hence,
    // the check that searchPackage() finds the two versions. If so, the test
    // is run; otherwise no test is run.  Need to find a better way to find
    // multiple versions of a package to test against.
    var oldEmacspeak = "38.0-5.fc20";
    var newEmacspeak = "39.0-1.fc20";
    var pkgs = packagekit.searchPackage("emacspseak");
    Iif (pkgs.length === 2 &&
        (pkgs[0].version === oldEmacspeak &&
         pkgs[1].version === newEmacspeak &&
         pkgs[1].data.indexOf("updates") !== -1)) {
 
        packagekit.updatePackage(pkgs[1].id);
        var currentPkgs = packagekit.searchPackage("emacspseak", "installed");
        jqUnit.assertEquals("Updating to " + newEmacspeak,
                            currentPkgs[0].version, newEmacspeak);
 
        // Restore to previous.
        packagekit.removePackage(currentPkgs[0].id);
    }
    else {
        jqUnit.assertTrue("Cannot test update of emacspeak as there are no " +
                          "updates", true);
    }
});