Ejemplo n.º 1
0
 public void InstallFailsTest()
 {
     string wd = "c:\\root\\project1";
     Uri registry = null;
     NpmFactory factory = new MockNpmFactory();
     NpmApi target = new NpmApi(factory, wd, registry);
     NpmPackage package = new NpmPackage("bogusmod", null);
     NpmException expected = MockTestData.ErrorInstallExpected();
     try
     {
         IEnumerable<INpmInstalledPackage> actual;
         actual = target.Install(package);
         Assert.Fail("Expected exception");
     }
     catch (NpmException ex)
     {
         Assert.IsNotNull(ex);
         Assert.AreEqual(expected.Message, ex.Message);
         Assert.AreEqual(expected.NpmCode, ex.NpmCode);
         Assert.AreEqual(expected.NpmErrno, ex.NpmErrno);
         Assert.AreEqual(expected.NpmFile, ex.NpmFile);
         Assert.AreEqual(expected.NpmPath, ex.NpmPath);
         Assert.AreEqual(expected.NpmType, ex.NpmType);
         Assert.AreEqual(expected.NpmSyscall, ex.NpmSyscall);
         Assert.AreEqual(expected.NpmSystem, ex.NpmSystem);
         Assert.AreEqual(expected.NpmCommand, ex.NpmCommand);
         Assert.AreEqual(expected.NpmNodeVersion, ex.NpmNodeVersion);
         Assert.AreEqual(expected.NpmNpmVersion, ex.NpmNpmVersion);
         Assert.AreEqual(expected.NpmMessage, ex.NpmMessage);
         Assert.AreEqual(expected.NpmArguments, ex.NpmArguments);
         Assert.AreEqual(expected.NpmCwd, ex.NpmCwd);
         Assert.AreEqual(expected.NpmVerbose, ex.NpmVerbose);
     }
 }
Ejemplo n.º 2
0
 public void NpmApiConstructorTest()
 {
     string wd = string.Empty;
     Uri registry = null;
     NpmApi target = new NpmApi(wd, registry);
     Assert.IsNotNull(target);
     Assert.IsInstanceOfType(target, typeof(NpmApi));
 }
Ejemplo n.º 3
0
 public void ViewTest()
 {
     string wd = string.Empty;
     Uri registry = null;
     NpmFactory factory = new MockNpmFactory();
     NpmApi target = new NpmApi(factory, wd, registry);
     string name = "view1";
     NpmRemotePackage expected = MockTestData.View1Expected();
     INpmRemotePackage actual;
     actual = target.View(name);
     Assert.AreEqual(expected, actual);
 }
Ejemplo n.º 4
0
 public void TestInstalledTest()
 {
     string wd = "c:\\root\\project1";
     Uri registry = null;
     NpmFactory factory = new MockNpmFactory();
     NpmApi target = new NpmApi(factory, wd, registry);
     NpmPackage package = MockTestData.List1MatchInstalledPackage();
     NpmInstalledPackage expected = MockTestData.List1MatchInstalledExpected();
     INpmInstalledPackage actual;
     actual = target.TestInstalled(package);
     if (expected == null)
     {
         Assert.AreEqual(expected, actual);
     }
     else
     {
         Assert.IsNotNull(actual);
         Assert.AreEqual(expected, actual);
     }
 }
Ejemplo n.º 5
0
 public void UninstallTest()
 {
     string wd = "c:\\root\\project1";
     Uri registry = null;
     NpmFactory factory = new MockNpmFactory();
     NpmApi target = new NpmApi(factory, wd, registry);
     string name = "uninstall1";
     bool expected = true;
     bool actual;
     actual = target.Uninstall(name);
     Assert.AreEqual(expected, actual);
 }
Ejemplo n.º 6
0
 public void UpdateTest()
 {
     string wd = "c:\\root\\project1";
     Uri registry = null;
     NpmFactory factory = new MockNpmFactory();
     NpmApi target = new NpmApi(factory, wd, registry);
     string name = "underscore";
     List<NpmInstalledPackage> expected = MockTestData.Install1Expected();
     IEnumerable<INpmInstalledPackage> actual;
     actual = target.Update(name);
     Assert.AreEqual(expected.Count, actual.Count());
     int index = 0;
     foreach (INpmInstalledPackage actualItem in actual)
     {
         Assert.AreEqual(expected[index], actualItem, "item value differs");
         index++;
     }
 }
Ejemplo n.º 7
0
 public void SearchTest()
 {
     string wd = string.Empty;
     Uri registry = null;
     NpmFactory factory = new MockNpmFactory();
     NpmApi target = new NpmApi(factory, wd, registry);
     string searchTerms = "search1";
     List<NpmSearchResultPackage> expected = MockTestData.SearchResult1Expected();
     IEnumerable<INpmSearchResultPackage> actual;
     actual = target.Search(searchTerms);
     Assert.IsNotNull(actual);
     Assert.AreEqual(expected.Count, actual.Count());
     int index = 0;
     foreach (INpmSearchResultPackage actualItem in actual)
     {
         Assert.AreEqual(expected[index], actualItem, "item value differs");
         index++;
     }
 }
Ejemplo n.º 8
0
 public void OutdatedTest1()
 {
     string wd = "c:\\root\\project1";
     Uri registry = null;
     NpmFactory factory = new MockNpmFactory();
     NpmApi target = new NpmApi(factory, wd, registry);
     List<NpmPackageDependency> expected = MockTestData.Outdated1Expected();
     IEnumerable<INpmPackageDependency> actual;
     actual = target.Outdated();
     Assert.IsNotNull(actual);
     Assert.AreEqual(expected.Count, actual.Count());
     int index = 0;
     foreach (INpmPackageDependency actualItem in actual)
     {
         Assert.AreEqual(expected[index], actualItem, "item value differs");
         index++;
     }
 }
Ejemplo n.º 9
0
 public void ListEmptyTest()
 {
     string wd = "c:\\root\\empty1";
     Uri registry = null;
     NpmFactory factory = new MockNpmFactory();
     NpmApi target = new NpmApi(factory, wd, registry);
     List<NpmInstalledPackage> expected = MockTestData.ListEmptyExpected();
     IEnumerable<INpmInstalledPackage> actual;
     actual = target.List();
     Assert.IsNotNull(actual);
     Assert.AreEqual(expected.Count, actual.Count());
     int index = 0;
     foreach (NpmInstalledPackage actualItem in actual)
     {
         Assert.AreEqual(expected[index], actualItem, "item value differs");
         index++;
     }
 }
Ejemplo n.º 10
0
 public void GetInstalledVersionTest()
 {
     string wd = string.Empty;
     Uri registry = null;
     NpmFactory factory = new MockNpmFactory();
     NpmApi target = new NpmApi(factory, wd, registry);
     string expected = MockTestData.Version1Expected();
     string actual;
     actual = target.GetInstalledVersion();
     Assert.AreEqual(expected, actual);
 }
Ejemplo n.º 11
0
        /// <summary>
        /// Exercise the NpmApi class
        /// </summary>
        /// <param name="wd">working directory path</param>
        /// <param name="installPath">node installation path</param>
        /// <param name="module">module name to use</param>
        /// <returns>true or false</returns>
        public static bool RunSample(string wd, string installPath, string module)
        {
            try
            {
                string uninstalledName = null;
                NpmApi npm = new NpmApi(wd);
                if (npm == null)
                {
                    Console.WriteLine("Failed to create NpmApi");
                    return false;
                }

                npm.NpmClient.InstallPath = installPath;

                string version = npm.GetInstalledVersion();
                INpmSearchResultPackage found = null;
                IEnumerable<INpmSearchResultPackage> searchResults = npm.Search(module);
                if (searchResults != null)
                {
                    foreach (INpmSearchResultPackage result in searchResults)
                    {
                        if (result.Name == module)
                        {
                            found = result;
                            break;
                        }
                    }
                }

                if (found == null)
                {
                    Console.WriteLine("Search failed to find '{0}'", module);
                    return false;
                }

                // install module as a dependency
                IEnumerable<INpmPackage> installed = npm.Install(found);
                if (installed == null || installed.Count() == 0)
                {
                    Console.WriteLine("Install failed for {0}", found.Name);
                    return false;
                }

                // list packages at parent
                IEnumerable<INpmInstalledPackage> installedPkg = npm.List();
                if (installedPkg == null)
                {
                    Console.WriteLine("List failed for {0}", found.Name);
                    return false;
                }

                // there should be at least 1 item since we installed one
                if (installedPkg.Count() == 0)
                {
                    Console.WriteLine("There are no packages listed");
                    return false;
                }

                // switch to dependency directory to uninstall a child
                npm.SetDependencyDirectory(found.Name);

                // remove a dependency
                IEnumerable<INpmInstalledPackage> installedChildren = npm.ListChildren();
                foreach (INpmInstalledPackage package in installedChildren)
                {
                    if (!npm.Uninstall(package.Name))
                    {
                        Console.WriteLine("Failed to uninstall dependency {0} of {1}", package.Name, found.Name);
                        return false;
                    }
                    else
                    {
                        uninstalledName = package.Name;
                    }

                    break;
                }

                // revert directory
                npm.SetDependencyDirectory(null);

                // check that it is reported as missing
                bool matchMissing = false;
                IEnumerable<INpmPackageDependency> outdated = npm.Outdated();
                if (outdated != null && outdated.Count() > 0)
                {
                    foreach (INpmPackageDependency outofdate in outdated)
                    {
                        if (outofdate.Name == uninstalledName &&
                            string.IsNullOrWhiteSpace(outofdate.Version))
                        {
                            matchMissing = true;
                            break;
                        }
                    }
                }

                if (!matchMissing)
                {
                    Console.WriteLine("Expected at least one outdated entry after uninstall of {0}", uninstalledName);
                    return false;
                }

                // now call update and check if it is fixed
                installedPkg = npm.Update(uninstalledName);
                if (installedPkg == null)
                {
                    Console.WriteLine("Update failed for {0}", uninstalledName);
                    return false;
                }

                // check that specified package is updated
                matchMissing = false;
                foreach (INpmInstalledPackage package in installedPkg)
                {
                    if (package.Name == uninstalledName)
                    {
                        matchMissing = true;
                        break;
                    }
                }

                if (!matchMissing)
                {
                    Console.WriteLine("Package not reported as updated for {0}", uninstalledName);
                    return false;
                }

                outdated = npm.Outdated();
                if (outdated != null && outdated.Count() > 0)
                {
                    Console.WriteLine("Expected no outdated entry after update of {0}", module);
                    return false;
                }

                Console.WriteLine("Success! {0} is installed.", module);
                return true;
            }
            catch (NpmException ex)
            {
                Console.WriteLine("Npm failed with exception.");
                Console.WriteLine("Message: " + ex.Message);
                if (ex.InnerException != null)
                {
                    Console.WriteLine("Inner message: " + ex.InnerException.Message);
                }
                else
                {
                    if (ex.NpmCode != null)
                    {
                        Console.WriteLine("Code: " + ex.NpmCode);
                    }

                    if (ex.NpmCommand != null)
                    {
                        Console.WriteLine("calling: " + ex.NpmCommand);
                    }
                }

                return false;
            }
        }