public void ProjectSourceFileCache_Ctor()
        {
            ProjectSourceFileCache cache;

            ConsoleLogger logger = new ConsoleLogger();
            using (ProjectFileReader projectFileReader = new ProjectFileReader(logger))
            {
                // Null/empty server project throws
                ExceptionHelper.ExpectArgumentNullExceptionStandard(() => cache = new ProjectSourceFileCache(null, "breadCrumb", logger, projectFileReader), "rootProjectPath");
                ExceptionHelper.ExpectArgumentNullExceptionStandard(() => cache = new ProjectSourceFileCache(string.Empty, "breadCrumb", logger, projectFileReader), "rootProjectPath");

                // Null/empty bread crumb file throws
                ExceptionHelper.ExpectArgumentNullExceptionStandard(() => cache = new ProjectSourceFileCache("proj", null, logger, projectFileReader), "historyFilePath");
                ExceptionHelper.ExpectArgumentNullExceptionStandard(() => cache = new ProjectSourceFileCache("proj", string.Empty, logger, projectFileReader), "historyFilePath");

                // Null logger throws
                ExceptionHelper.ExpectArgumentNullExceptionStandard(() => cache = new ProjectSourceFileCache("proj", "breadCrumb", null, projectFileReader), "logger");

                // Null projctFileReader throws
                ExceptionHelper.ExpectArgumentNullExceptionStandard(() => cache = new ProjectSourceFileCache("proj", "breadCrumb", logger, null), "projectFileReader");

                // Valid ctor succeeds
                cache = new ProjectSourceFileCache("proj", "breadCrumb", logger, projectFileReader);
            }
        }
        public void ProjectSourceFileCache_Ctor()
        {
            ProjectSourceFileCache cache;

            ConsoleLogger logger = new ConsoleLogger();

            using (ProjectFileReader projectFileReader = new ProjectFileReader(logger))
            {
                // Null/empty server project throws
                ExceptionHelper.ExpectArgumentNullExceptionStandard(() => cache = new ProjectSourceFileCache(null, "breadCrumb", logger, projectFileReader), "rootProjectPath");
                ExceptionHelper.ExpectArgumentNullExceptionStandard(() => cache = new ProjectSourceFileCache(string.Empty, "breadCrumb", logger, projectFileReader), "rootProjectPath");

                // Null/empty bread crumb file throws
                ExceptionHelper.ExpectArgumentNullExceptionStandard(() => cache = new ProjectSourceFileCache("proj", null, logger, projectFileReader), "historyFilePath");
                ExceptionHelper.ExpectArgumentNullExceptionStandard(() => cache = new ProjectSourceFileCache("proj", string.Empty, logger, projectFileReader), "historyFilePath");

                // Null logger throws
                ExceptionHelper.ExpectArgumentNullExceptionStandard(() => cache = new ProjectSourceFileCache("proj", "breadCrumb", null, projectFileReader), "logger");

                // Null projctFileReader throws
                ExceptionHelper.ExpectArgumentNullExceptionStandard(() => cache = new ProjectSourceFileCache("proj", "breadCrumb", logger, null), "projectFileReader");

                // Valid ctor succeeds
                cache = new ProjectSourceFileCache("proj", "breadCrumb", logger, projectFileReader);
            }
        }
        public void ProjectFileReader_Bad_Project_File_Warns()
        {
            ConsoleLogger logger = new ConsoleLogger();
            using (ProjectFileReader projectFileReader = new ProjectFileReader(logger))
            {
                string badProjectPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString() + ".csproj");
                File.WriteAllText(badProjectPath, "neener neener");

                try
                {
                    projectFileReader.LoadProject(badProjectPath);

                    // Simulate the exception so we get the exact text
                    string warningMessage = null;
                    try
                    {
                        Engine engine = new Engine();
                        Project project = new Project(engine);
                        project.Load(badProjectPath);

                    }
                    catch (InvalidProjectFileException ipfe)
                    {
                        warningMessage = string.Format(CultureInfo.CurrentCulture, Resource.Failed_To_Open_Project, badProjectPath, ipfe.Message);
                    }

                    TestHelper.AssertContainsWarnings(logger, new string[] { warningMessage });
                }
                finally
                {
                    File.Delete(badProjectPath);
                }
            }
        }
        public void SharedSourceFiles_Methods()
        {
            string projectPath = null;
            string outputPath = null;
            TestHelper.GetProjectPaths("SFT", out projectPath, out outputPath);
            string clientProjectPath = CodeGenHelper.ClientClassLibProjectPath(projectPath);
            List<string> sourceFiles = CodeGenHelper.ClientClassLibSourceFiles(clientProjectPath);
            ConsoleLogger logger = new ConsoleLogger();
            FilenameMap filenameMap = new FilenameMap();

            using (SourceFileLocationService locationService = new SourceFileLocationService(new[] { new PdbSourceFileProviderFactory(/*symbolSearchPath*/ null, logger) }, filenameMap))
            {
                SharedSourceFiles ssf = new SharedSourceFiles(locationService, filenameMap, sourceFiles);

                int[] fileIds = ssf.GetSharedFileIds(CodeMemberKey.CreateMethodKey(typeof(TestValidator).GetMethod("IsValid")));
                Assert.IsNotNull(fileIds, "Expected TestValidator.IsValid to have non-null file ID's because it is shared");
                Assert.AreEqual(1, fileIds.Length, "Expected TestValidator.IsValid to be found in exactly one file");
                Assert.IsTrue(filenameMap[fileIds[0]].Contains("TestValidator.linked.cs"), "Expected TestValidator.IsValid to be in TestValidator.linked.cs");

                fileIds = ssf.GetSharedFileIds(CodeMemberKey.CreateMethodKey(typeof(TestEntity).GetMethod("ServerAndClientMethod")));
                Assert.IsNotNull(fileIds, "Expected TestEntity.ServerAndClientMethod to have non-null file ID's because it is shared");
                Assert.AreEqual(1, fileIds.Length, "Expected TestEntity.ServerAndClientMethod to be found in exactly one file");
                Assert.IsTrue(filenameMap[fileIds[0]].Contains("TestEntity.linked.cs"), "Expected TestEntity.ServerAndClientMethod to be in TestEntity.linked.cs");

                fileIds = ssf.GetSharedFileIds(CodeMemberKey.CreateMethodKey(typeof(TestEntity).GetMethod("ServerMethod")));
                Assert.IsNull(fileIds, "Expected TestEntity.ServerMethod to have null file ids");

                fileIds = ssf.GetSharedFileIds(CodeMemberKey.CreateMethodKey(typeof(TestValidatorServer).GetMethod("IsValid")));
                Assert.IsNull(fileIds, "Expected TestValidatorServer.IsValid to have null file ids");
            }
        }
示例#5
0
        public void ProjectFileReader_Bad_Project_File_Warns()
        {
            ConsoleLogger logger = new ConsoleLogger();

            using (ProjectFileReader projectFileReader = new ProjectFileReader(logger))
            {
                string badProjectPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString() + ".csproj");
                File.WriteAllText(badProjectPath, "neener neener");

                try
                {
                    projectFileReader.LoadProject(badProjectPath);

                    // Simulate the exception so we get the exact text
                    string warningMessage = null;
                    try
                    {
                        Engine  engine  = new Engine();
                        Project project = new Project(engine);
                        project.Load(badProjectPath);
                    }
                    catch (InvalidProjectFileException ipfe)
                    {
                        warningMessage = string.Format(CultureInfo.CurrentCulture, Resource.Failed_To_Open_Project, badProjectPath, ipfe.Message);
                    }

                    TestHelper.AssertContainsWarnings(logger, new string[] { warningMessage });
                }
                finally
                {
                    File.Delete(badProjectPath);
                }
            }
        }
        public void SharedSourceFiles_Types()
        {
            string projectPath = null;
            string outputPath = null;
            TestHelper.GetProjectPaths("SFT", out projectPath, out outputPath);
            string clientProjectPath = CodeGenHelper.ClientClassLibProjectPath(projectPath);
            List<string> sourceFiles = CodeGenHelper.ClientClassLibSourceFiles(clientProjectPath);
            ConsoleLogger logger = new ConsoleLogger();
            FilenameMap filenameMap = new FilenameMap();

            using (SourceFileLocationService locationService = new SourceFileLocationService(new[] { new PdbSourceFileProviderFactory(/*symbolSearchPath*/ null, logger) }, filenameMap))
            {
                SharedSourceFiles ssf = new SharedSourceFiles(locationService, filenameMap, sourceFiles);

                int[] fileIds = ssf.GetSharedFileIds(CodeMemberKey.CreateTypeKey(typeof(TestEntity)));
                Assert.IsNotNull(fileIds, "Expected TestEntity to have non-null file ID's because it is shared");
                Assert.AreEqual(2, fileIds.Length, "Expected TestEntity to be found in exactly 2 files");
                foreach (int i in fileIds)
                {
                    string file = filenameMap[i];
                    Assert.IsTrue(file.Contains("TestEntity.linked.cs") || file.Contains("TestEntity.reverse.linked.cs"), "Expected exactly these 2 files to be shared");
                }

                fileIds = ssf.GetSharedFileIds(CodeMemberKey.CreateTypeKey(typeof(TestValidator)));
                Assert.IsNotNull(fileIds, "Expected TestValidator to have non-null file ID's because it is shared");
                Assert.AreEqual(1, fileIds.Length, "Expected TestValidator to be found in exactly one file");
                Assert.IsTrue(filenameMap[fileIds[0]].Contains("TestValidator.linked.cs"), "expected this to be the sole shared file for TestValidator");

                fileIds = ssf.GetSharedFileIds(CodeMemberKey.CreateTypeKey(typeof(DomainService)));
                Assert.IsNull(fileIds, "Expected DomainService to have no shared file ids");

                fileIds = ssf.GetSharedFileIds(CodeMemberKey.CreateTypeKey(typeof(TestValidatorServer)));
                Assert.IsNull(fileIds, "Expected DomainService to have no shared file ids");
            }
        }
示例#7
0
        public void ProjectFileReader_Ctor()
        {
            ConsoleLogger logger = new ConsoleLogger();

            ExceptionHelper.ExpectArgumentNullExceptionStandard(() => new ProjectFileReader(null), "logger");

            using (ProjectFileReader projectFileReader = new ProjectFileReader(logger))
            {
            }
        }
        public void ProjectFileReader_Ctor()
        {
            ConsoleLogger logger = new ConsoleLogger();

            ExceptionHelper.ExpectArgumentNullExceptionStandard(() => new ProjectFileReader(null), "logger");

            using (ProjectFileReader projectFileReader = new ProjectFileReader(logger))
            {
            }
        }
 public void ProjectFileReader_Dispose_Multiple()
 {
     ConsoleLogger logger = new ConsoleLogger();
     ProjectFileReader projectFileReader;
     using (projectFileReader = new ProjectFileReader(logger))
     {
     }
     projectFileReader.Dispose();
     projectFileReader.Dispose();
     System.GC.Collect();
     System.GC.WaitForPendingFinalizers();
     projectFileReader.Dispose();
 }
示例#10
0
        public void ProjectFileReader_Dispose_Multiple()
        {
            ConsoleLogger     logger = new ConsoleLogger();
            ProjectFileReader projectFileReader;

            using (projectFileReader = new ProjectFileReader(logger))
            {
            }
            projectFileReader.Dispose();
            projectFileReader.Dispose();
            System.GC.Collect();
            System.GC.WaitForPendingFinalizers();
            projectFileReader.Dispose();
        }
        public void ProjectSourceFileCache_Indexer()
        {
            ConsoleLogger logger = new ConsoleLogger();
            using (ProjectFileReader projectFileReader = new ProjectFileReader(logger))
            {

                ProjectSourceFileCache cache = new ProjectSourceFileCache("proj", "breadCrumb", logger, projectFileReader);

                // First access allocates empty instance
                Assert.IsNotNull(cache.SourceFilesByProject);
                Assert.AreEqual(0, cache.SourceFilesByProject.Count, "Expected empty cache");

                // Indexer setter can be called
                cache["proj"] = new string[] { "a", "b", "c" };

                // Which invalidates the currency of the cache
                Assert.IsFalse(cache.IsFileCacheCurrent, "Adding to the cache should have marked the file cache as out of date.");

                // And indexer getter returns the inputs
                IEnumerable<string> files = cache["proj"];
                Assert.IsNotNull(files);
                Assert.AreEqual(3, files.Count());
                Assert.IsTrue(files.Contains("a"));
                Assert.IsTrue(files.Contains("b"));
                Assert.IsTrue(files.Contains("c"));

                // Cache is case insensitive.  Should overwrite entry differing only in case
                cache["PrOj"] = new string[] { "d", "e" };
                Assert.AreEqual(1, cache.SourceFilesByProject.Count, "Key differing only in case should have overwritten existing one");
                files = cache["PrOj"];
                Assert.IsNotNull(files);

                // And can read out again in different case
                files = cache["pRoJ"];
                Assert.IsNotNull(files);

                // Null value clears entry
                cache["proj"] = null;
                files = cache["proj"];
                Assert.IsNull(files);

                // ArgNull exception on null/empty index setter
                ExceptionHelper.ExpectArgumentNullExceptionStandard(() => cache[null] = new string[] { "a" }, "projectPath");
                ExceptionHelper.ExpectArgumentNullExceptionStandard(() => cache[string.Empty] = new string[] { "a" }, "projectPath");

                // ArgNull exception on null/empty index getter
                ExceptionHelper.ExpectArgumentNullExceptionStandard(() => files = cache[null], "projectPath");
                ExceptionHelper.ExpectArgumentNullExceptionStandard(() => files = cache[string.Empty], "projectPath");
            }
        }
        public void ProjectSourceFileCache_Indexer()
        {
            ConsoleLogger logger = new ConsoleLogger();

            using (ProjectFileReader projectFileReader = new ProjectFileReader(logger))
            {
                ProjectSourceFileCache cache = new ProjectSourceFileCache("proj", "breadCrumb", logger, projectFileReader);

                // First access allocates empty instance
                Assert.IsNotNull(cache.SourceFilesByProject);
                Assert.AreEqual(0, cache.SourceFilesByProject.Count, "Expected empty cache");

                // Indexer setter can be called
                cache["proj"] = new string[] { "a", "b", "c" };

                // Which invalidates the currency of the cache
                Assert.IsFalse(cache.IsFileCacheCurrent, "Adding to the cache should have marked the file cache as out of date.");

                // And indexer getter returns the inputs
                IEnumerable <string> files = cache["proj"];
                Assert.IsNotNull(files);
                Assert.AreEqual(3, files.Count());
                Assert.IsTrue(files.Contains("a"));
                Assert.IsTrue(files.Contains("b"));
                Assert.IsTrue(files.Contains("c"));

                // Cache is case insensitive.  Should overwrite entry differing only in case
                cache["PrOj"] = new string[] { "d", "e" };
                Assert.AreEqual(1, cache.SourceFilesByProject.Count, "Key differing only in case should have overwritten existing one");
                files = cache["PrOj"];
                Assert.IsNotNull(files);

                // And can read out again in different case
                files = cache["pRoJ"];
                Assert.IsNotNull(files);

                // Null value clears entry
                cache["proj"] = null;
                files         = cache["proj"];
                Assert.IsNull(files);

                // ArgNull exception on null/empty index setter
                ExceptionHelper.ExpectArgumentNullExceptionStandard(() => cache[null]         = new string[] { "a" }, "projectPath");
                ExceptionHelper.ExpectArgumentNullExceptionStandard(() => cache[string.Empty] = new string[] { "a" }, "projectPath");

                // ArgNull exception on null/empty index getter
                ExceptionHelper.ExpectArgumentNullExceptionStandard(() => files = cache[null], "projectPath");
                ExceptionHelper.ExpectArgumentNullExceptionStandard(() => files = cache[string.Empty], "projectPath");
            }
        }
        public void ProjectSourceFileCache_Failed_BreadCrumb_Save_Warns()
        {
            ConsoleLogger logger = new ConsoleLogger();

            using (ProjectFileReader projectFileReader = new ProjectFileReader(logger))
            {
                string projectPath    = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString() + ".csproj");
                string breadCrumbPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString() + ".txt");
                string csFile         = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString() + ".cs");

                // Need file on disk only for timestamp
                File.WriteAllText(projectPath, "neener neener");
                File.WriteAllText(breadCrumbPath, "bread crumbs");
                File.WriteAllText(csFile, "//");

                try
                {
                    ProjectSourceFileCache cache = new ProjectSourceFileCache(projectPath, breadCrumbPath, logger, projectFileReader);
                    cache.SourceFilesByProject[projectPath] = new string[] { csFile };

                    // Setup for failure
                    File.SetAttributes(breadCrumbPath, FileAttributes.ReadOnly);

                    // Ask to write to readonly file -- should fail
                    cache.SaveCacheToFile();

                    // Simulate the exception so we get the exact text
                    string warningMessage = null;
                    try
                    {
                        // this should fail
                        File.WriteAllText(breadCrumbPath, "stuff");
                    }
                    catch (UnauthorizedAccessException uae)
                    {
                        warningMessage = string.Format(CultureInfo.CurrentCulture, Resource.Failed_To_Write_File, breadCrumbPath, uae.Message);
                    }

                    TestHelper.AssertContainsWarnings(logger, new string[] { warningMessage });
                }
                finally
                {
                    File.Delete(projectPath);
                    File.SetAttributes(breadCrumbPath, File.GetAttributes(breadCrumbPath) & ~FileAttributes.ReadOnly);
                    File.Delete(breadCrumbPath);
                    File.Delete(csFile);
                }
            }
        }
        public void ProjectSourceFileCache_AllKnownProjects()
        {
            ConsoleLogger logger = new ConsoleLogger();

            using (ProjectFileReader projectFileReader = new ProjectFileReader(logger))
            {
                ProjectSourceFileCache cache = new ProjectSourceFileCache("proj", "breadCrumb", logger, projectFileReader);

                cache["p1"] = new string[] { "a", "b" };
                cache["p2"] = new string[] { "c" };

                IEnumerable <string> projects = cache.GetAllKnownProjects();
                Assert.IsNotNull(projects);
                Assert.AreEqual(2, projects.Count());
                Assert.IsTrue(projects.Contains("p1"));
                Assert.IsTrue(projects.Contains("p2"));
            }
        }
        public void ProjectFileReader_Nonexistent_Project_File_Warns()
        {
            ConsoleLogger logger = new ConsoleLogger();
            string badProjectPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString() + ".csproj");
            using (ProjectFileReader projectFileReader = new ProjectFileReader(logger))
            {
                try
                {
                    projectFileReader.LoadProject(badProjectPath);

                    string warningMessage = string.Format(CultureInfo.CurrentCulture, Resource.Project_Does_Not_Exist, badProjectPath);

                    TestHelper.AssertContainsWarnings(logger, new string[] { warningMessage });
                }
                finally
                {
                    File.Delete(badProjectPath);
                }
            }
        }
示例#16
0
        public void ProjectFileReader_Nonexistent_Project_File_Warns()
        {
            ConsoleLogger logger         = new ConsoleLogger();
            string        badProjectPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString() + ".csproj");

            using (ProjectFileReader projectFileReader = new ProjectFileReader(logger))
            {
                try
                {
                    projectFileReader.LoadProject(badProjectPath);

                    string warningMessage = string.Format(CultureInfo.CurrentCulture, Resource.Project_Does_Not_Exist, badProjectPath);

                    TestHelper.AssertContainsWarnings(logger, new string[] { warningMessage });
                }
                finally
                {
                    File.Delete(badProjectPath);
                }
            }
        }
        public void ProjectSourceFileCache_Nonexistent_Project_File_Warns()
        {
            ConsoleLogger logger         = new ConsoleLogger();
            string        badProjectPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString() + ".csproj");

            using (ProjectFileReader projectFileReader = new ProjectFileReader(logger))
            {
                try
                {
                    ProjectSourceFileCache cache    = new ProjectSourceFileCache(badProjectPath, "breadCrumb", logger, projectFileReader);
                    IEnumerable <string>   projects = cache.GetAllKnownProjects();

                    string warningMessage = string.Format(CultureInfo.CurrentCulture, Resource.Project_Does_Not_Exist, badProjectPath);

                    TestHelper.AssertContainsWarnings(logger, new string[] { warningMessage });
                }
                finally
                {
                    File.Delete(badProjectPath);
                }
            }
        }
        public void ProjectFileReader_Bad_Project_File_Warns()
        {
            ConsoleLogger logger = new ConsoleLogger();

            using (ProjectFileReader projectFileReader = new ProjectFileReader(logger))
            {
                string badProjectPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString() + ".csproj");
                File.WriteAllText(badProjectPath, "neener neener");

                try
                {
                    projectFileReader.LoadProject(badProjectPath);

                    // Simulate the exception so we get the exact text
                    string warningMessage = TestHelper.GetFailedToOpenProjectMessage(badProjectPath);
                    TestHelper.AssertContainsWarnings(logger, new string[] { warningMessage });
                }
                finally
                {
                    File.Delete(badProjectPath);
                }
            }
        }
        public void ProjectSourceFileCache_Loads_Real_Project()
        {
            string projectPath = null;
            string outputPath  = null;

            TestHelper.GetProjectPaths("SSFC", out projectPath, out outputPath);
            string        serverProjectPath = CodeGenHelper.ServerClassLibProjectPath(projectPath);
            string        breadCrumbFile    = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString() + ".txt");
            ConsoleLogger logger            = new ConsoleLogger();

            using (ProjectFileReader projectFileReader = new ProjectFileReader(logger))
            {
                try
                {
                    // Instantiate the cache.
                    ProjectSourceFileCache cache = new ProjectSourceFileCache(serverProjectPath, breadCrumbFile, logger, projectFileReader);

                    // Initially, it will not have loaded anything
                    Assert.AreEqual(0, cache.SourceFilesByProject.Count);

                    // -------------------------------------------------------------
                    // Validate cache is loaded correctly from .csproj files
                    // -------------------------------------------------------------
                    this.ValidateProjectSourceFileCache(cache, projectPath);

                    // Ask to write out the breadcrumb file
                    Assert.IsFalse(File.Exists(breadCrumbFile));
                    bool success = cache.SaveCacheToFile();
                    Assert.IsTrue(success);
                    Assert.IsTrue(File.Exists(breadCrumbFile));

                    // Clear the cache and force it to read from breadcrumb
                    cache.SourceFilesByProject.Clear();

                    success = cache.LoadCacheFromFile();
                    Assert.IsTrue(success, "Failed to load from breadCrumb file");
                    Assert.IsTrue(cache.IsFileCacheCurrent, "Loading from file should have marked cache as current with file.");

                    // -------------------------------------------------------------
                    // Validate cache is loaded correctly from breadcrumb file
                    // -------------------------------------------------------------
                    this.ValidateProjectSourceFileCache(cache, projectPath);

                    // Now mark the breadcrumb file as if it had been written before the ServerClassLib project
                    cache.SourceFilesByProject.Clear();

                    DateTime serverLastWriteTime       = File.GetLastWriteTime(serverProjectPath);
                    DateTime beforeServerLastWriteTime = new DateTime(serverLastWriteTime.Ticks - 1000);
                    File.SetLastWriteTime(breadCrumbFile, beforeServerLastWriteTime);

                    // -------------------------------------------------------------
                    // Validate cache is *not* loaded if timestamp is before project's
                    // -------------------------------------------------------------
                    success = cache.LoadCacheFromFile();
                    Assert.IsFalse(success, "Expected breadCrumbFile time stamp to be caught and reject load");
                    Assert.IsFalse(cache.IsFileCacheCurrent, "Failed load from file should have marked cache as *not* current with file.");

                    // Cache should still be empty
                    Assert.AreEqual(0, cache.SourceFilesByProject.Count);

                    // -------------------------------------------------------------
                    // Validate cache loaded in presence of out-of-date breadcrumb
                    // file loads correctly
                    // -------------------------------------------------------------
                    this.ValidateProjectSourceFileCache(cache, projectPath);

                    Assert.IsFalse(cache.IsFileCacheCurrent, "Loading from project should have marked cache as *not* current with file.");
                }
                finally
                {
                    if (File.Exists(breadCrumbFile))
                    {
                        File.Delete(breadCrumbFile);
                    }
                }
            }
        }
        public void ProjectSourceFileCache_Nonexistent_Project_File_SourceFiles_Warns()
        {
            ConsoleLogger logger = new ConsoleLogger();
            using (ProjectFileReader projectFileReader = new ProjectFileReader(logger))
            {
                string badProjectPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString() + ".csproj");

                try
                {
                    ProjectSourceFileCache cache = new ProjectSourceFileCache("proj", "breadCrumb", logger, projectFileReader);
                    IEnumerable<string> sourceFiles = projectFileReader.LoadSourceFilesFromProject(badProjectPath);

                    string warningMessage = string.Format(CultureInfo.CurrentCulture, Resource.Project_Does_Not_Exist, badProjectPath);

                    TestHelper.AssertContainsWarnings(logger, new string[] { warningMessage });

                }
                finally
                {
                    File.Delete(badProjectPath);
                }
            }
        }
        public void ProjectSourceFileCache_Loads_Real_Project()
        {
            string projectPath = null;
            string outputPath = null;
            TestHelper.GetProjectPaths("SSFC", out projectPath, out outputPath);
            string serverProjectPath = CodeGenHelper.ServerClassLibProjectPath(projectPath);
            string breadCrumbFile = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString() + ".txt");
            ConsoleLogger logger = new ConsoleLogger();
            using (ProjectFileReader projectFileReader = new ProjectFileReader(logger))
            {

                try
                {
                    // Instantiate the cache.
                    ProjectSourceFileCache cache = new ProjectSourceFileCache(serverProjectPath, breadCrumbFile, logger, projectFileReader);

                    // Initially, it will not have loaded anything
                    Assert.AreEqual(0, cache.SourceFilesByProject.Count);

                    // -------------------------------------------------------------
                    // Validate cache is loaded correctly from .csproj files
                    // -------------------------------------------------------------
                    this.ValidateProjectSourceFileCache(cache, projectPath);

                    // Ask to write out the breadcrumb file
                    Assert.IsFalse(File.Exists(breadCrumbFile));
                    bool success = cache.SaveCacheToFile();
                    Assert.IsTrue(success);
                    Assert.IsTrue(File.Exists(breadCrumbFile));

                    // Clear the cache and force it to read from breadcrumb
                    cache.SourceFilesByProject.Clear();

                    success = cache.LoadCacheFromFile();
                    Assert.IsTrue(success, "Failed to load from breadCrumb file");
                    Assert.IsTrue(cache.IsFileCacheCurrent, "Loading from file should have marked cache as current with file.");

                    // -------------------------------------------------------------
                    // Validate cache is loaded correctly from breadcrumb file
                    // -------------------------------------------------------------
                    this.ValidateProjectSourceFileCache(cache, projectPath);

                    // Now mark the breadcrumb file as if it had been written before the ServerClassLib project
                    cache.SourceFilesByProject.Clear();

                    DateTime serverLastWriteTime = File.GetLastWriteTime(serverProjectPath);
                    DateTime beforeServerLastWriteTime = new DateTime(serverLastWriteTime.Ticks - 1000);
                    File.SetLastWriteTime(breadCrumbFile, beforeServerLastWriteTime);

                    // -------------------------------------------------------------
                    // Validate cache is *not* loaded if timestamp is before project's
                    // -------------------------------------------------------------
                    success = cache.LoadCacheFromFile();
                    Assert.IsFalse(success, "Expected breadCrumbFile time stamp to be caught and reject load");
                    Assert.IsFalse(cache.IsFileCacheCurrent, "Failed load from file should have marked cache as *not* current with file.");

                    // Cache should still be empty
                    Assert.AreEqual(0, cache.SourceFilesByProject.Count);

                    // -------------------------------------------------------------
                    // Validate cache loaded in presence of out-of-date breadcrumb
                    // file loads correctly
                    // -------------------------------------------------------------
                    this.ValidateProjectSourceFileCache(cache, projectPath);

                    Assert.IsFalse(cache.IsFileCacheCurrent, "Loading from project should have marked cache as *not* current with file.");
                }
                finally
                {
                    if (File.Exists(breadCrumbFile))
                        File.Delete(breadCrumbFile);
                }
            }
        }
        public void ProjectSourceFileCache_AllKnownProjects()
        {
            ConsoleLogger logger = new ConsoleLogger();
            using (ProjectFileReader projectFileReader = new ProjectFileReader(logger))
            {
                ProjectSourceFileCache cache = new ProjectSourceFileCache("proj", "breadCrumb", logger, projectFileReader);

                cache["p1"] = new string[] { "a", "b" };
                cache["p2"] = new string[] { "c" };

                IEnumerable<string> projects = cache.GetAllKnownProjects();
                Assert.IsNotNull(projects);
                Assert.AreEqual(2, projects.Count());
                Assert.IsTrue(projects.Contains("p1"));
                Assert.IsTrue(projects.Contains("p2"));
            }
        }
        public void ProjectSourceFileCache_Failed_BreadCrumb_Save_Warns()
        {
            ConsoleLogger logger = new ConsoleLogger();
            using (ProjectFileReader projectFileReader = new ProjectFileReader(logger))
            {
                string projectPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString() + ".csproj");
                string breadCrumbPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString() + ".txt");
                string csFile = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString() + ".cs");

                // Need file on disk only for timestamp
                File.WriteAllText(projectPath, "neener neener");
                File.WriteAllText(breadCrumbPath, "bread crumbs");
                File.WriteAllText(csFile, "//");

                try
                {
                    ProjectSourceFileCache cache = new ProjectSourceFileCache(projectPath, breadCrumbPath, logger, projectFileReader);
                    cache.SourceFilesByProject[projectPath] = new string[] { csFile };

                    // Setup for failure
                    File.SetAttributes(breadCrumbPath, FileAttributes.ReadOnly);

                    // Ask to write to readonly file -- should fail
                    cache.SaveCacheToFile();

                    // Simulate the exception so we get the exact text
                    string warningMessage = null;
                    try
                    {

                        // this should fail
                        File.WriteAllText(breadCrumbPath, "stuff");
                    }
                    catch (UnauthorizedAccessException uae)
                    {
                        warningMessage = string.Format(CultureInfo.CurrentCulture, Resource.Failed_To_Write_File, breadCrumbPath, uae.Message);
                    }

                    TestHelper.AssertContainsWarnings(logger, new string[] { warningMessage });

                }
                finally
                {
                    File.Delete(projectPath);
                    File.SetAttributes(breadCrumbPath, File.GetAttributes(breadCrumbPath) & ~FileAttributes.ReadOnly);
                    File.Delete(breadCrumbPath);
                    File.Delete(csFile);
                }
            }
        }