Example #1
0
        public async Task Test_Log()
        {
            CShell shell = new CShell();

            shell.cd(testFolder);
            var commandResult = await shell.Cmd("dir /b TestA.txt").Execute(true);
        }
Example #2
0
        public void Test_ChangeFolder()
        {
            Environment.CurrentDirectory = testFolder;

            var shell = new CShell();

            shell.cd(@"subfolder\subfolder2");
            Assert.AreEqual(subFolder2, shell.CurrentFolder.FullName, "currentFolder relative path failed");
            shell.cd(@"..\subfolder2");
            Assert.AreEqual(subFolder2, shell.CurrentFolder.FullName, "currentFolder relative path failed2");
            Assert.AreEqual(2, shell.FolderHistory.Count, "relative non-navigation shouldn't have created history record");
            shell.cd(@"..\..");
            Assert.AreEqual(testFolder, shell.CurrentFolder.FullName, "currentFolder relative path failed3");
            Assert.AreEqual(3, shell.FolderHistory.Count, "history ignored on relative path");
            shell.cd(subFolder2);
            Assert.AreEqual(subFolder2, shell.CurrentFolder.FullName, "absolute path failed");
        }
Example #3
0
        public async Task Test_Bash()
        {
            CShell shell = new CShell();

            shell.cd(testFolder);
            var result = await shell.Bash("ls TestA.txt").AsString();

            Assert.AreEqual("TestA.txt", result.Trim(), "AsString");
        }
Example #4
0
        public async Task Test_AsXml()
        {
            CShell shell = new CShell();

            shell.cd(testFolder);

            var record = await shell.ReadFile("TestB.txt").AsXml <TestRecord>();

            Assert.AreEqual("Joe Smith", record.Name, "name is wrong");
            Assert.AreEqual(42, record.Age, "age is wrong");
        }
Example #5
0
        public void Test_CurrentDirectory()
        {
            Environment.CurrentDirectory = testFolder;

            CShell shell = new CShell();

            shell.cd(subFolder);
            Assert.AreEqual(subFolder, Environment.CurrentDirectory, "environment path not changed");
            Assert.AreEqual(subFolder, shell.CurrentFolder.FullName, "currentFolder not changed");
            Assert.AreEqual(subFolder, shell.FolderHistory[1], "history should be updated");
            Assert.AreEqual(testFolder, shell.FolderHistory[0], "history should be updated");
            Assert.AreEqual(2, shell.FolderHistory.Count(), "history should be two");
        }
Example #6
0
        public void Test_move()
        {
            Environment.CurrentDirectory = testFolder;

            var shell = new CShell();

            shell.cd(testFolder);
            File.WriteAllText(Path.Combine(shell.CurrentFolder.FullName, "test.txt"), "test");

            shell.move("test.txt", subFolder);
            string path2 = Path.Combine(subFolder, "test.txt");

            Assert.IsTrue(File.Exists(path2));

            shell.cd(subFolder);
            shell.move("test.txt", Path.Combine("..", "test2.txt"));
            string path3 = Path.Combine(testFolder, "test2.txt");

            Assert.IsTrue(File.Exists(path3));

            File.Delete(Path.Combine(testFolder, "test2.txt"));
        }
Example #7
0
        public void Test_rdRecursive()
        {
            Environment.CurrentDirectory = testFolder;
            var    shell = new CShell();
            string xyz   = Path.Combine(testFolder, "xyz");

            if (Directory.Exists(xyz))
            {
                Directory.Delete(xyz, true);
            }

            shell.md("xyz");
            Assert.IsTrue(Directory.EnumerateDirectories(testFolder).Where(path => Path.GetFileName(path) == "xyz").Any(), "xyz not created");

            shell.cd("xyz");
            shell.md("xyz");
            Assert.IsTrue(Directory.EnumerateDirectories(testFolder).Where(path => Path.GetFileName(path) == "xyz").Any(), "xyz not created");

            shell.cd(testFolder);
            shell.rd("xyz", true);
            Assert.IsFalse(Directory.EnumerateDirectories(testFolder).Where(path => Path.GetFileName(path) == "xyz").Any(), "xyz not created");
        }
Example #8
0
        public async Task Test_AsString()
        {
            CShell shell = new CShell();

            shell.cd(testFolder);

            var result = await shell.Run("cmd", "/c", "echo this is a yo yo").AsString();

            Assert.AreEqual("this is a yo yo", result.Trim(), "AsString");

            var record = await shell.ReadFile("TestA.txt").AsString();

            var text = File.ReadAllText(Path.Combine(testFolder, "TestA.Txt"));

            Assert.AreEqual(record, text, "AsString");
        }
Example #9
0
        public async Task Test_AsResult()
        {
            CShell shell = new CShell();

            shell.ThrowOnError = false;
            shell.cd(testFolder);

            var result = await shell.ReadFile("TestA.txt").AsResult();

            var text = File.ReadAllText(Path.Combine(testFolder, "TestA.Txt"));

            Assert.AreEqual(text, result.StandardOutput, "result stdout");
            Assert.AreEqual("", result.StandardError, "result stderr");


            var badResult = await shell.ReadFile("sdfsdffd.txt").AsResult();

            Assert.AreEqual("", badResult.StandardOutput, "result stdout");
            Assert.AreEqual("The system cannot find the file specified.", badResult.StandardError.Trim(), "result stderr");
        }
Example #10
0
        public async Task Test_AsJson()
        {
            CShell shell = new CShell();

            shell.cd(testFolder);

            var record = await shell.ReadFile("TestA.txt").AsJson <TestRecord>();

            Assert.AreEqual("Joe Smith", record.Name, "name is wrong");
            Assert.AreEqual(42, record.Age, "age is wrong");

            JObject record2 = (JObject)await shell.ReadFile("TestA.txt").AsJson();

            Assert.AreEqual("Joe Smith", (string)record2["name"], "JOBject name is wrong");
            Assert.AreEqual(42, (int)record2["age"], "JOBject age is wrong");

            dynamic record3 = await shell.ReadFile("TestA.txt").AsJson();

            Assert.AreEqual("Joe Smith", (string)record3.name, "dynamic name is wrong");
            Assert.AreEqual(42, (int)record3.age, "dynamic age is wrong");
        }
Example #11
0
        public void Test_MoveCopyFolder()
        {
            Environment.CurrentDirectory = testFolder;

            var shell = new CShell();

            shell.cd(testFolder);
            if (Directory.Exists(shell.ResolvePath("test2")))
            {
                shell.rd("test2", recursive: true);
            }

            shell.copy("subfolder", "test2");
            Assert.IsTrue(File.Exists(Path.Combine(testFolder, "test2", "TestC.txt")));
            Assert.IsTrue(File.Exists(Path.Combine(testFolder, "test2", "subfolder2", "TestE.txt")));
            shell.move("test2", "test3");
            Assert.IsTrue(!Directory.Exists("test2"));
            Assert.IsTrue(File.Exists(Path.Combine(testFolder, "test3", "TestC.txt")));
            Assert.IsTrue(File.Exists(Path.Combine(testFolder, "test3", "subfolder2", "TestE.txt")));

            shell.rd("test3", recursive: true);
        }
Example #12
0
        public async Task Test_AsFile()
        {
            CShell shell = new CShell();

            shell.ThrowOnError = false;
            shell.cd(testFolder);

            var tmpOut = Path.GetTempFileName();
            var tmpErr = Path.GetTempFileName();

            var result = await shell.ReadFile("TestA.txt").AsFile(tmpOut);

            var stdout = File.ReadAllText(tmpOut);

            Assert.AreEqual(stdout, result.StandardOutput, "result stdout");

            var result2 = await shell.ReadFile("TestAsdfsdf.txt").AsFile(tmpOut, tmpErr);

            var stdout2 = File.ReadAllText(tmpOut);
            var stderr2 = File.ReadAllText(tmpErr);

            Assert.AreEqual(stdout2, result2.StandardOutput, "result stdout");
            Assert.AreEqual(stderr2, result2.StandardError, "result stderr");
        }