Ejemplo n.º 1
0
        public void InferScriptTagTest_NoTagInferenceSource()
        {
            TagInferenceSource source       = TagInferenceSource.None;
            List <string>      regexFormats = ScriptTagProcessingTest.regex;
            string             scriptName   = "CR 343423 Test Script.sql";
            string             scriptPath   = @"C:\test";
            string             expected     = string.Empty;
            string             actual;

            actual = ScriptTagProcessing.InferScriptTag(source, regexFormats, scriptName, scriptPath);
            Assert.AreEqual(expected, actual);
        }
Ejemplo n.º 2
0
        public void InferScriptTagTest_NullRegexListSet()
        {
            string             scriptPathAndName = @"C:\mypath\path2\CR2123456-test script.sql";
            string             scriptContents    = Properties.Resources.TagFromContents;
            List <string>      regexFormats      = null;
            TagInferenceSource source            = TagInferenceSource.TextOverName;
            string             expected          = "";
            string             actual;

            actual = ScriptTagProcessing.InferScriptTag(scriptPathAndName, scriptContents, regexFormats, source);
            Assert.AreEqual(expected, actual);
        }
Ejemplo n.º 3
0
        public void InferScriptTagTest_TextOverName_GetFromName()
        {
            string             scriptPathAndName = @"C:\mypath\path2\CR2123456-test script.sql";
            string             scriptContents    = "This is content that doesn't have a single tag in it.";
            List <string>      regexFormats      = ScriptTagProcessingTest.regex;
            TagInferenceSource source            = TagInferenceSource.TextOverName;
            string             expected          = "CR2123456";
            string             actual;

            actual = ScriptTagProcessing.InferScriptTag(scriptPathAndName, scriptContents, regexFormats, source);
            Assert.AreEqual(expected, actual);
        }
Ejemplo n.º 4
0
        public void InferScriptTagTest_NameOverText_GetFromText()
        {
            string             scriptPathAndName = @"C:\mypath\path2\test script.sql";
            string             scriptContents    = Properties.Resources.TagFromContents;
            List <string>      regexFormats      = ScriptTagProcessingTest.regex;
            TagInferenceSource source            = TagInferenceSource.NameOverText;
            string             expected          = "CR987654";
            string             actual;

            actual = ScriptTagProcessing.InferScriptTag(scriptPathAndName, scriptContents, regexFormats, source);
            Assert.AreEqual(expected, actual);
        }
Ejemplo n.º 5
0
        public void InferScriptTagTest_FileDoesntExist()
        {
            TagInferenceSource source       = TagInferenceSource.TextOverName;
            List <string>      regexFormats = ScriptTagProcessingTest.regex;
            string             scriptName   = Path.GetTempPath() + Guid.NewGuid().ToString();
            string             scriptPath   = Path.GetDirectoryName(scriptName);
            string             expected     = "";
            string             actual;

            actual = ScriptTagProcessing.InferScriptTag(source, regexFormats, scriptName, scriptPath);

            Assert.AreEqual(expected, actual);
        }
        /// <summary>
        /// Gets the script tag from a file name or file contents
        /// </summary>
        /// <param name="source">The enum designating where to get the tag from</param>>
        /// <param name="regexFormats">The regular expression formats used to extract the teg</param>
        /// <param name="scriptName">The name of the file</param>
        /// <param name="scriptPath">The path to the file</param>
        /// <returns>The extracted script tag or empty string if not found</returns>
        public static string InferScriptTag(TagInferenceSource source, List <string> regexFormats, string scriptName, string scriptPath)
        {
            log.LogDebug($"InferScriptTag: TagInferenceSource for {scriptName} is {Enum.GetName(typeof(TagInferenceSource),source)}");
            if (source == TagInferenceSource.None)
            {
                return(string.Empty);
            }

            if (regexFormats == null)
            {
                log.LogWarning($"InferScriptTag: RegularExpression formats is null when processing {scriptName}");
                return(string.Empty);
            }

            try
            {
                scriptName = Path.GetFileName(scriptName);

                string tmpTag;

                //Do this easier check first to avoid opening the file is possible...
                if (source == TagInferenceSource.ScriptName || source == TagInferenceSource.NameOverText)
                {
                    tmpTag = InferScriptTagFromFileName(scriptName, regexFormats);
                    if (tmpTag.Length > 0)
                    {
                        return(tmpTag);
                    }
                }


                //If we get here, we will need to get the file contents...
                if (!File.Exists(Path.Combine(scriptPath, scriptName)))
                {
                    log.LogWarning($"Unable to find file for Script Tag Inference for file {scriptName} in path {scriptPath}");
                    return(string.Empty);
                }

                string contents = File.ReadAllText(Path.Combine(scriptPath, scriptName));
                tmpTag = InferScriptTag(scriptName, contents, regexFormats, source);
                if (tmpTag.Length > 0)
                {
                    return(tmpTag);
                }
            }
            catch (Exception exe)
            {
                log.LogError(exe, $"Error Inferring Script Tag for file {scriptName} in path {scriptPath}.");
            }
            return(string.Empty);
        }
Ejemplo n.º 7
0
        public void InferScriptTagTest_SourceIsNone()
        {
            string             scriptPathAndName = @"C:\mypath\path2\NothingHereCR2123456-test script.sql";
            string             scriptContents    = @"There is nothing here that 
should match either the CR number 1234 content match or the 
P number 1234 content match";
            List <string>      regexFormats      = ScriptTagProcessingTest.regex;
            TagInferenceSource source            = TagInferenceSource.None;
            string             expected          = "";
            string             actual;

            actual = ScriptTagProcessing.InferScriptTag(scriptPathAndName, scriptContents, regexFormats, source);
            Assert.AreEqual(expected, actual);
        }
Ejemplo n.º 8
0
        public void InferScriptTagTest_TextOverNameNothingFound()
        {
            TagInferenceSource source       = TagInferenceSource.ScriptName;
            List <string>      regexFormats = ScriptTagProcessingTest.regex;
            string             scriptName   = Path.GetTempPath() + @"empty tagless test me.sql";

            File.WriteAllText(scriptName, "This doesn't have a tag in it");
            string scriptPath = Path.GetDirectoryName(scriptName);
            string expected   = "";
            string actual;

            actual = ScriptTagProcessing.InferScriptTag(source, regexFormats, scriptName, scriptPath);

            Assert.AreEqual(expected, actual);
        }
Ejemplo n.º 9
0
        public void InferScriptTagTest_NullRegex()
        {
            TagInferenceSource source       = TagInferenceSource.ScriptName;
            List <string>      regexFormats = null;
            string             scriptName   = Path.GetTempPath() + @"CR 123456 test me.sql";

            File.WriteAllText(scriptName, Properties.Resources.TagFromContents);
            string scriptPath = Path.GetDirectoryName(scriptName);
            string expected   = string.Empty;
            string actual;

            actual = ScriptTagProcessing.InferScriptTag(source, regexFormats, scriptName, scriptPath);

            if (File.Exists(scriptName))
            {
                File.Delete(scriptName);
            }

            Assert.AreEqual(expected, actual);
        }
        /// <summary>
        /// Used to infer script tags for an entire build package
        /// </summary>
        /// <param name="buildData" >The SqlSyncBuildData object for the current package</param>
        /// <param name="projectPath">The path that the SBM is unpacked to</param>
        /// <param name="regexFormats">The Regex formats that will be used to try to extract script tags</param>
        /// <param name="source">The enum designating where to get the tag from</param>
        /// <returns>Boolean as to whether the inference worked</returns>
        public static bool InferScriptTags(ref SqlSyncBuildData buildData, string projectPath, List <string> regexFormats, TagInferenceSource source)
        {
            bool   atLeastOneUpdated = false;
            string tmpTag            = string.Empty;

            foreach (SqlSyncBuildData.ScriptRow row in buildData.Script)
            {
                tmpTag = InferScriptTag(source, regexFormats, row.FileName, projectPath);
                if (tmpTag.Length > 0)
                {
                    row.Tag           = tmpTag;
                    atLeastOneUpdated = true;
                }
            }

            if (atLeastOneUpdated)
            {
                buildData.AcceptChanges();
            }

            return(atLeastOneUpdated);
        }
        /// <summary>
        /// Gets a script tag from the text of the script or from the path name
        /// </summary>
        /// <param name="scriptPathAndName">The full name and path of the script</param>
        /// <param name="scriptContents">The text of the script</param>
        /// <param name="regexFormats">The regular expression formats used to extract the teg</param>
        /// <param name="source">The enum designating where to get the tag from</param>
        /// <returns>The extracted script tag or empty string if not found</returns>
        public static string InferScriptTag(string scriptPathAndName, string scriptContents, List <string> regexFormats, TagInferenceSource source)
        {
            if (regexFormats == null)
            {
                return(string.Empty);
            }

            if (source == TagInferenceSource.None)
            {
                return(string.Empty);
            }


            string fileContentTag = InferScriptTagFromFileContents(scriptContents, regexFormats);

            string fileNameTag = InferScriptTagFromFileName(scriptPathAndName, regexFormats);


            //Not able to get anything...
            if (fileContentTag.Length == 0 && fileNameTag.Length == 0)
            {
                return(string.Empty);
            }

            if (fileNameTag.Length > 0 && source == TagInferenceSource.ScriptName)
            {
                return(fileNameTag);
            }

            if (fileContentTag.Length > 0 && source == TagInferenceSource.ScriptText)
            {
                return(fileContentTag);
            }

            if (source == TagInferenceSource.TextOverName)
            {
                if (fileContentTag.Length > 0)
                {
                    return(fileContentTag);
                }
                else if (fileNameTag.Length > 0)
                {
                    return(fileNameTag);
                }
            }

            if (source == TagInferenceSource.NameOverText)
            {
                if (fileNameTag.Length > 0)
                {
                    return(fileNameTag);
                }
                else if (fileContentTag.Length > 0)
                {
                    return(fileContentTag);
                }
            }

            return(string.Empty);
        }