示例#1
0
        /// ------------------------------------------------------------------------------------
        /// <summary>
        /// Gets the files in the given directory.
        /// </summary>
        /// <param name="sPath">The directory path.</param>
        /// <param name="searchPattern">The search string to match against the names of files in
        /// path. The parameter cannot end in two periods ("..") or contain two periods ("..")
        /// followed by DirectorySeparatorChar or AltDirectorySeparatorChar, nor can it contain
        /// any of the characters in InvalidPathChars.</param>
        /// <returns>list of files</returns>
        /// ------------------------------------------------------------------------------------
        string[] IFileOS.GetFilesInDirectory(string sPath, string searchPattern)
        {
            FileUtils.AssertValidFilePath(sPath);
            if (searchPattern == null)
            {
                throw new ArgumentNullException("searchPattern");
            }
            // These next two lines look a little strange, but I think we do this to deal with
            // normalization issues.
            int iDir = ExistingDirectories.IndexOf(sPath);

            if (iDir == -1)
            {
                sPath = sPath.TrimEnd(Path.DirectorySeparatorChar);
                iDir  = ExistingDirectories.IndexOf(sPath);
            }
            string existingDir = iDir >= 0 ? ExistingDirectories[iDir] : null;

            Regex regex = null;

            if (searchPattern != "*")
            {
                searchPattern = searchPattern.Replace(".", @"\.").Replace("*", ".*").Replace("?", ".");
                searchPattern = searchPattern.Replace("+", @"\+").Replace("$", @"\$").Replace("(", @"\(").Replace(")", @"\)");
                regex         = new Regex(searchPattern);
            }

            List <string> files = new List <string>(m_existingFiles.Count);

            foreach (string file in m_existingFiles.Keys)
            {
                if (regex != null)
                {
                    string fileName = Path.GetFileName(file);
                    Match  m        = regex.Match(fileName);
                    if (m.Value != fileName)
                    {
                        continue;
                    }
                }

                string fileDir = Path.GetDirectoryName(file);
                if (fileDir == String.Empty)
                {
                    // Some of our tests just add files with no path and expect them to be
                    // treated as existing in any existing directory
                    files.Add(Path.Combine(existingDir, file));
                }
                else if (fileDir == sPath)
                {
                    files.Add(file);
                }
            }
            if (files.Count == 0 && existingDir == null)
            {
                throw new DirectoryNotFoundException();
            }
            return(files.ToArray());
        }
示例#2
0
        /// ------------------------------------------------------------------------------------
        /// <summary>
        /// Adds the given filename to the collection of files that should be considered to
        /// exist and set its contents so it can be read.
        /// </summary>
        /// <param name="filename">The filename (may or may not include path).</param>
        /// <param name="contents">The contents of the file</param>
        /// <param name="encoding">File encoding</param>
        /// ------------------------------------------------------------------------------------
        public void AddFile(string filename, string contents, Encoding encoding)
        {
            FileUtils.AssertValidFilePath(filename);
            string dir = Path.GetDirectoryName(filename);

            if (!string.IsNullOrEmpty(dir) && !((IFileOS)this).DirectoryExists(dir))
            {
                ExistingDirectories.Add(dir);                 // Theoretically, this should add containing folders recursively.
            }
            m_existingFiles[filename] = new MockFile(contents, encoding);
        }
示例#3
0
 /// ------------------------------------------------------------------------------------
 /// <summary>
 /// Determines whether the specified file exists. Looks in m_existingFiles.Keys without
 /// making any adjustment for case or differences in normalization.
 /// </summary>
 /// <param name="sPath">The file path.</param>
 /// ------------------------------------------------------------------------------------
 bool IFileOS.FileExists(string sPath)
 {
     FileUtils.AssertValidFilePath(sPath);
     if (String.IsNullOrEmpty(sPath))
     {
         return(false);
     }
     // Can't use Contains because it takes care of normalization mismatches, but for
     // the purposes of these tests, we want to simulate an Operating System which doesn't
     // (e.g., MS Windows).
     foreach (string sExistingFile in m_existingFiles.Keys)
     {
         if (sExistingFile == sPath)
         {
             return(true);
         }
     }
     return(false);
 }
示例#4
0
 /// ------------------------------------------------------------------------------------
 /// <summary>
 /// Simulates getting an exclusive (write) file lock on the file having the given filename.
 /// </summary>
 /// <param name="filename">The filename (must have been added previously).</param>
 /// ------------------------------------------------------------------------------------
 public void LockFile(string filename)
 {
     FileUtils.AssertValidFilePath(filename);
     m_existingFiles[filename].Lock = FileLockType.Write;
 }