Exemplo n.º 1
0
        private static string GenerateUrl(JsTestOption option, JsTest.JsTestCommand.JsCommand command, string Folder, string File, string function = null)
        {
            string url = option.RequestPrefix + "/exe";

            Dictionary <string, string> para = new Dictionary <string, string>();
            string cmdname = command.ToString();

            if (!string.IsNullOrEmpty(cmdname))
            {
                para.Add("command", cmdname);
            }

            if (!string.IsNullOrEmpty(Folder))
            {
                para.Add("folder", Folder);
            }

            if (!string.IsNullOrEmpty(File))
            {
                para.Add("file", File);
            }

            if (!string.IsNullOrEmpty(function))
            {
                para.Add("function", function);
            }

            return(Lib.Helper.UrlHelper.AppendQueryString(url, para));
        }
Exemplo n.º 2
0
        public static HashSet <string> ListTestFolders(RenderContext context, JsTestOption options)
        {
            HashSet <string> result = new HashSet <string>(StringComparer.OrdinalIgnoreCase);

            var root = options.GetDiskRoot(context);

            if (!string.IsNullOrEmpty(options.TestFolder))
            {
                var allsubs = System.IO.Directory.GetDirectories(root, options.TestFolder, System.IO.SearchOption.AllDirectories);

                foreach (var item in allsubs)
                {
                    System.IO.DirectoryInfo info = new System.IO.DirectoryInfo(item);

                    string fullname = info.FullName;
                    fullname = fullname.Substring(root.Length);
                    fullname = fullname.Replace("\\", "/");

                    var path = options.FolderPath(context);

                    if (!string.IsNullOrEmpty(path) && fullname.ToLower().StartsWith(path))
                    {
                        fullname = fullname.Substring(path.Length + 1);
                    }
                    result.Add(fullname);
                }
            }

            return(result);
        }
Exemplo n.º 3
0
        public static RenderRespnose Render(RenderContext Context, JsTestOption option)
        {
            string root = option.GetDiskRoot(Context);

            string         relativeurl = RenderEngine.ParseRelativeUrl(Context.Request.RawRelativeUrl, option);
            RenderRespnose response    = new RenderRespnose();

            var command = JsTestHelper.ParseCommand(relativeurl);

            if (command.IsJs)
            {
                RenderJs(Context, option, root, response, command);
            }
            else
            {
                response.ContentType = "text/html";

                if (command.Command == JsTestCommand.JsCommand.view)
                {
                    RenderTestView(Context, option, response, command);
                }
                else if (command.Command == JsTestCommand.JsCommand.run)
                {
                    RenderTestRun(Context, option, response, command);
                }
            }
            return(response);
        }
Exemplo n.º 4
0
        public static HashSet <string> ListAllTestFiles(RenderContext context, JsTestOption option, string folder)
        {
            HashSet <string> Result = new HashSet <string>();

            HashSet <string> Folders;

            if (string.IsNullOrEmpty(folder))
            {
                Folders = ListTestFolders(context, option);
            }
            else
            {
                Folders = new HashSet <string>();
                Folders.Add(folder);
            }

            foreach (var item in Folders)
            {
                var folderfiles = ListFolderFiles(context, option, item);
                foreach (var file in folderfiles)
                {
                    string jsfile = Lib.Helper.IOHelper.CombinePath(file.Folder, file.file);
                    Result.Add(jsfile);
                }
            }
            return(Result);
        }
Exemplo n.º 5
0
        public static HashSet <JsFilePath> ListFolderFiles(RenderContext context, JsTestOption option, string Folder)
        {
            HashSet <JsFilePath> result = new HashSet <JsFilePath>();

            var root = option.GetDiskRoot(context);

            var path = option.FolderPath(context);

            if (!string.IsNullOrEmpty(path))
            {
                root = Lib.Helper.IOHelper.CombinePath(root, path);
            }

            if (!string.IsNullOrEmpty(Folder))
            {
                var fullfolder = Lib.Helper.IOHelper.CombinePath(root, Folder);

                if (System.IO.Directory.Exists(fullfolder))
                {
                    var allfiles = System.IO.Directory.GetFiles(fullfolder, "*.js", System.IO.SearchOption.AllDirectories);

                    foreach (var item in allfiles)
                    {
                        System.IO.FileInfo info = new System.IO.FileInfo(item);

                        if (!option.AssertJs.Contains(info.Name))
                        {
                            var jsPath = new JsFilePath();
                            if (!string.IsNullOrEmpty(Folder))
                            {
                                var index = info.FullName.IndexOf(Folder.Replace("/", "\\"), StringComparison.OrdinalIgnoreCase);
                                if (index > -1)
                                {
                                    var relativePath = info.FullName.Substring(index);

                                    jsPath.Folder = System.IO.Path.GetDirectoryName(relativePath);
                                    jsPath.file   = System.IO.Path.GetFileName(relativePath);
                                }
                                else
                                {
                                    jsPath.Folder = Folder;
                                    jsPath.file   = info.Name;
                                }
                            }
                            else
                            {
                                jsPath.file = info.Name;
                            }
                            result.Add(jsPath);
                        }
                    }
                }
            }

            return(result);
        }
Exemplo n.º 6
0
        public static int CountTest(RenderContext context, JsTestOption option, List <string> Folders)
        {
            int count = 0;

            foreach (var item in Folders)
            {
                count += CountTest(context, option, item);
            }
            return(count);
        }
Exemplo n.º 7
0
        private static string GetBlockJs(JsTestOption option, string js)
        {
            int start = js.IndexOf(option.FunctionBlockStart);
            int end   = js.IndexOf(option.FunctionBlockEnd);

            if (start > -1 && end > -1 && end > start)
            {
                return(js.Substring(start + option.FunctionBlockStart.Length, end - start - option.FunctionBlockStart.Length));
            }
            return(js);
        }
Exemplo n.º 8
0
        public static int CountTest(RenderContext context, JsTestOption option, string folder)
        {
            int count    = 0;
            var allfiles = ListFolderFiles(context, option, folder);

            foreach (var file in allfiles)
            {
                var funcs = ListFileFunctions(context, option, file.Folder, file.file);
                count += funcs.Count();
            }
            return(count);
        }
Exemplo n.º 9
0
        public static string ParseRelativeUrl(string RawRelativeUrl, JsTestOption option)
        {
            string RelativeUrl = RawRelativeUrl;

            if (!string.IsNullOrEmpty(option.RequestPrefix))
            {
                if (RelativeUrl.ToLower().StartsWith(option.RequestPrefix))
                {
                    RelativeUrl = RelativeUrl.Substring(option.RequestPrefix.Length);
                }
            }
            return(RelativeUrl);
        }
Exemplo n.º 10
0
        public static HashSet <string> GetReferenceJs(RenderContext context, JsTestOption option, string folder)
        {
            if (string.IsNullOrWhiteSpace(folder) || folder == "\\" || folder == "/")
            {
                return(GetReferenceJs(context, option));
            }

            HashSet <string> result = new HashSet <string>();

            string root = option.GetDiskRoot(context);

            System.IO.DirectoryInfo basedir = new System.IO.DirectoryInfo(root);

            var prepath = option.FolderPath(context);

            if (!string.IsNullOrEmpty(prepath))
            {
                root = Lib.Helper.IOHelper.CombinePath(root, prepath);
            }

            System.IO.DirectoryInfo rootdir = new System.IO.DirectoryInfo(root);

            folder = Lib.Helper.IOHelper.CombinePath(root, folder);

            var currentdir = new System.IO.DirectoryInfo(folder);

            while (currentdir.Exists && currentdir.FullName.Length > rootdir.FullName.Length)
            {
                var file = Lib.Helper.IOHelper.CombinePath(currentdir.FullName, option.JsReferenceFileName);
                if (System.IO.File.Exists(file))
                {
                    var alllines = System.IO.File.ReadAllLines(file);
                    foreach (var line in alllines)
                    {
                        if (line != null)
                        {
                            var lineresult = FindRelativeFileName(basedir, line, currentdir);

                            if (!string.IsNullOrEmpty(lineresult))
                            {
                                result.Add(lineresult);
                            }
                        }
                    }
                }

                currentdir = currentdir.Parent;
            }

            return(result);
        }
Exemplo n.º 11
0
        public bool ShouldTryHandle(Kooboo.Data.Context.RenderContext context, JsTestOption Options)
        {
            if (string.IsNullOrEmpty(Options.RequestPrefix))
            {
                return(true);
            }
            string RelativeUrl = context.Request.RawRelativeUrl;

            if (RelativeUrl.ToLower().StartsWith(Options.RequestPrefix.ToLower()))
            {
                return(true);
            }
            return(false);
        }
Exemplo n.º 12
0
        public static string GetInfoHtml(RenderContext context, JsTestOption option)
        {
            var    approot = Kooboo.Data.AppSettings.RootPath;
            string root    = Lib.Helper.IOHelper.CombinePath(approot, _defaultFolder);

            root = Lib.Helper.IOHelper.CombinePath(root, "kbtest");
            string FullFileName = Lib.Helper.IOHelper.CombinePath(root, "info.html");

            if (System.IO.File.Exists(FullFileName))
            {
                return(System.IO.File.ReadAllText(FullFileName));
            }
            return(null);
        }
Exemplo n.º 13
0
        public static string GetStartHtml(RenderContext context, JsTestOption option)
        {
            var    approot = Kooboo.Data.AppSettings.RootPath;
            string root    = Lib.Helper.IOHelper.CombinePath(approot, _defaultFolder);

            root = Lib.Helper.IOHelper.CombinePath(root, "kbtest");
            string FullFileName = Lib.Helper.IOHelper.CombinePath(root, "start.html");

            if (System.IO.File.Exists(FullFileName))
            {
                string html = System.IO.File.ReadAllText(FullFileName);
                return(html.Replace("{home}", option.RequestPrefix));
            }
            return(null);
        }
Exemplo n.º 14
0
        public static string RenderJs(JsTestOption option, string js, string function, string retryurl)
        {
            int start = js.IndexOf(option.FunctionBlockStart);
            int end   = js.IndexOf(option.FunctionBlockEnd);

            if (start > -1 && end > -1 && end > start)
            {
                string block = js.Substring(start + option.FunctionBlockStart.Length, end - start - option.FunctionBlockStart.Length);

                string beginstring = js.Substring(0, start);
                string endstring   = js.Substring(end + option.FunctionBlockEnd.Length + 1);

                var renderblock = RenderBlockRun(block, function, retryurl);
                return(beginstring + renderblock + endstring);
            }
            else
            {
                return(RenderBlockRun(js, function, retryurl));
            }
        }
Exemplo n.º 15
0
        public static HashSet <string> ListFileFunctions(RenderContext context, JsTestOption option, string folder, string file)
        {
            var root = option.GetDiskRoot(context);

            string prepath = option.FolderPath(context);

            if (!string.IsNullOrEmpty(prepath))
            {
                root = Lib.Helper.IOHelper.CombinePath(root, prepath);
            }

            if (!string.IsNullOrEmpty(folder))
            {
                var fullfolder = Lib.Helper.IOHelper.CombinePath(root, folder);
                var fullfile   = Lib.Helper.IOHelper.CombinePath(fullfolder, file);

                var alltext = System.IO.File.ReadAllText(fullfile);
                var block   = GetBlockJs(option, alltext);
                return(Lib.Helper.JintHelper.ListFunctionNames(block));
            }
            return(new HashSet <string>());
        }
Exemplo n.º 16
0
        public static HashSet <string> GetReferenceJs(RenderContext context, JsTestOption option)
        {
            HashSet <string> result = new HashSet <string>();

            var allfolders = ListTestFolders(context, option);

            foreach (var folder in allfolders)
            {
                if (!string.IsNullOrWhiteSpace(folder) && folder != "\\" && folder != "/")
                {
                    var folderresult = GetReferenceJs(context, option, folder);
                    foreach (var item in folderresult)
                    {
                        if (!result.Contains(item.ToLower()))
                        {
                            result.Add(item.ToLower());
                        }
                    }
                }
            }
            return(result);
        }
Exemplo n.º 17
0
        private static string GeneratejsFile(JsTestOption option, string FileName, JsTest.JsTestCommand.JsCommand command = JsTestCommand.JsCommand.view, string function = null)
        {
            string prefix = option.RequestPrefix;

            if (!prefix.EndsWith("/") && !prefix.EndsWith("\\"))
            {
                prefix = prefix + "/";
            }

            if (FileName.StartsWith("/") || FileName.StartsWith("\\"))
            {
                FileName = FileName.Substring(1);
            }

            string name = Lib.Helper.UrlHelper.Combine(prefix, FileName);

            name += "?command=" + command.ToString();
            if (!string.IsNullOrEmpty(function))
            {
                name += "&function=" + function;
            }

            return(name);
        }
Exemplo n.º 18
0
        public static void RenderJs(RenderContext Context, JsTestOption option, string root, RenderRespnose response, JsTestCommand command)
        {
            response.ContentType = "application/javascript";

            if (!string.IsNullOrEmpty(command.JsPath))
            {
                string filename = command.JsPath.Replace("/", "\\");

                if (filename.IndexOf("?") > -1)
                {
                    filename = filename.Substring(0, filename.IndexOf("?"));
                }

                if (filename.StartsWith("\\"))
                {
                    filename = filename.Substring(1);
                }

                string fullname = root;
                string prepath  = option.FolderPath(Context);

                if (!string.IsNullOrEmpty(prepath))
                {
                    fullname = IOHelper.CombinePath(root, prepath);
                }

                fullname = IOHelper.CombinePath(fullname, filename);


                if (!System.IO.File.Exists(fullname))
                {
                    // This is to make sure the render of assert js...
                    foreach (var item in option.AssertJs)
                    {
                        if (filename.EndsWith(item))
                        {
                            fullname = System.IO.Path.Combine(Kooboo.Data.AppSettings.RootPath, "/_admin/kbtest/" + item);
                        }
                    }
                }

                if (System.IO.File.Exists(fullname))
                {
                    string baserelarive = GetRelative(prepath, command.JsPath);


                    if (command.Command == JsTestCommand.JsCommand.run)
                    {
                        string retryurl  = string.Empty;
                        string rawfolder = filename;
                        int    lastslash = rawfolder.LastIndexOf("\\");
                        if (lastslash > -1)
                        {
                            string folder     = rawfolder.Substring(0, lastslash);
                            string jsfilename = rawfolder.Substring(lastslash + 1);
                            retryurl = GenerateUrl(option, JsTestCommand.JsCommand.run, folder, jsfilename);
                        }

                        // TODO: Render the k commands.
                        var alltext = IOHelper.ReadAllText(fullname);
                        alltext = RenderServerSide(alltext, root, Context, baserelarive);

                        response.Body = RenderJs(option, alltext, command.Function, retryurl);
                    }
                    else
                    {
                        var alltext = IOHelper.ReadAllText(fullname);
                        alltext = RenderServerSide(alltext, root, Context, baserelarive);

                        response.Body = alltext;
                    }
                }
            }
        }
Exemplo n.º 19
0
        public static void RenderTestView(RenderContext Context, JsTestOption option, RenderRespnose response, JsTestCommand command)
        {
            if (string.IsNullOrEmpty(command.Folder) || command.Folder == "\\" || command.Folder == "/")
            {
                // find the start html.
                var    starthtml = JsTestHelper.GetStartHtml(Context, option);
                string url       = GenerateUrl(option, JsTestCommand.JsCommand.run, null, null);

                var    folders = JsTestHelper.ListTestFolders(Context, option);
                var    count   = JsTestHelper.CountTest(Context, option, folders.ToList());
                string html    = "<h3><a href='" + url + "'>run all tests</a>  (" + count.ToString() + " tests)</h3>\r\n";

                html += "<ul>";
                foreach (var item in folders)
                {
                    var files     = JsTestHelper.ListAllTestFiles(Context, option, item);
                    int testcount = JsTestHelper.CountTest(Context, option, item);

                    if (testcount > 0)
                    {
                        html += "<li>";

                        url   = GenerateUrl(option, JsTestCommand.JsCommand.run, item, null);
                        html += "<a href='" + url + "'>run</a> || ";

                        url   = GenerateUrl(option, JsTestCommand.JsCommand.view, item, null);
                        html += "<a href='" + url + "'>view</a> || ";

                        html += files.Count.ToString() + " files || ";
                        html += testcount.ToString() + " tests || Folder: ";
                        html += item;
                        html += "</li>";
                    }
                }
                html += "</ul>";

                string output = starthtml.Replace(JsTestHelper.PlaceHolder, html);

                string info = JsTestHelper.GetInfoHtml(Context, option);

                output = output.Replace("<div id=\"information\"></div>", info);

                response.Body = output;
            }

            else
            {
                if (string.IsNullOrEmpty(command.File))
                {
                    // view folder...
                    var starthtml = JsTestHelper.GetStartHtml(Context, option);

                    var files = JsTestHelper.ListFolderFiles(Context, option, command.Folder);

                    var count = JsTestHelper.CountTest(Context, option, command.Folder);

                    string url  = GenerateUrl(option, JsTestCommand.JsCommand.run, command.Folder, null);
                    string html = "<h3><a href='" + url + "'>run all tests</a>  (" + count.ToString() + " tests)</h3>\r\n";

                    html += "<ul>";

                    foreach (var item in files)
                    {
                        var functions = JsTestHelper.ListFileFunctions(Context, option, item.Folder, item.file);

                        html += "<li>";

                        url   = GenerateUrl(option, JsTestCommand.JsCommand.run, item.Folder, item.file);
                        html += "<a href='" + url + "'>run</a> || ";

                        url   = GenerateUrl(option, JsTestCommand.JsCommand.view, item.Folder, item.file);
                        html += "<a href='" + url + "'>view</a>  || ";

                        html += functions.Count.ToString() + " tests || File: " + System.IO.Path.Combine(item.Folder, item.file);

                        html += "</li>";
                    }
                    html += "</ul>";


                    response.Body = starthtml.Replace(JsTestHelper.PlaceHolder, html);
                }
                else
                {
                    //view file.
                    var    starthtml = JsTestHelper.GetStartHtml(Context, option);
                    string url       = GenerateUrl(option, JsTestCommand.JsCommand.run, command.Folder, command.File);

                    var functions = JsTestHelper.ListFileFunctions(Context, option, command.Folder, command.File);

                    string html = "<h3><a href='" + url + "'>run all tests</a>  (" + functions.Count.ToString() + " tests)</h3>\r\n";

                    html += "<ul>";

                    foreach (var item in functions)
                    {
                        url   = GenerateUrl(option, JsTestCommand.JsCommand.run, command.Folder, command.File, item);
                        html += "<li><a href='" + url + "'>run</a> || Test: " + item + "</li>";
                    }
                    html         += "</ul>";
                    response.Body = starthtml.Replace(JsTestHelper.PlaceHolder, html);
                }
            }
        }
Exemplo n.º 20
0
        private static void RenderTestRun(RenderContext Context, JsTestOption option, RenderRespnose response, JsTestCommand command)
        {
            var    starthtml = JsTestHelper.GetStartHtml(Context, option);
            string html      = "<div>";

            html += "\r\n<script src='/_admin/kbtest/expect.js'></script>";
            html += "\r\n<script src='/_admin/kbtest/mock.js'></script>";

            var references = JsTestHelper.GetReferenceJs(Context, option, command.Folder);

            foreach (var item in references)
            {
                html += "\r\n<script src='" + item + "'></script>";
            }

            if (string.IsNullOrEmpty(command.Folder) || command.Folder == "\\" || command.Folder == "/")
            {
                // run all.
                var allfiles = JsTestHelper.ListAllTestFiles(Context, option, null);
                foreach (var file in allfiles)
                {
                    string fileurl = GeneratejsFile(option, file, JsTestCommand.JsCommand.run);
                    html += "\r\n<script src='" + fileurl + "'></script>";
                }
            }

            else if (string.IsNullOrEmpty(command.File))
            {
                // run folder.
                var allfiles = JsTestHelper.ListAllTestFiles(Context, option, command.Folder);
                foreach (var file in allfiles)
                {
                    string fileurl = GeneratejsFile(option, file, JsTestCommand.JsCommand.run);
                    html += "\r\n<script src='" + fileurl + "'></script>";
                }
            }
            else
            {
                if (string.IsNullOrEmpty(command.Function))
                {
                    // run file.
                    string filename = Lib.Helper.IOHelper.CombinePath(command.Folder, command.File);

                    string fileurl = GeneratejsFile(option, filename, JsTestCommand.JsCommand.run);
                    html += "\r\n<script src='" + fileurl + "'></script>";
                }
                else
                {
                    // run function.
                    string filename = Lib.Helper.IOHelper.CombinePath(command.Folder, command.File);

                    html += "<div><h4>You are running one unit test, open console to view any errors</h4></div>";

                    string fileurl = GeneratejsFile(option, filename, JsTestCommand.JsCommand.run, command.Function);
                    html += "\r\n<script src='" + fileurl + "'></script>";
                }
            }

            html         += "</div>";
            response.Body = starthtml.Replace(JsTestHelper.PlaceHolder, html);
        }
Exemplo n.º 21
0
 public JsTestMiddleWare(JsTestOption options)
 {
     this.options = options;
 }