public void DirtyCleanScenario() { ResGenDependencies cache = new ResGenDependencies(); string resx = CreateSampleResx(); string stateFile = FileUtilities.GetTemporaryFile(); try { // A newly created cache is not dirty. Assert.False(cache.IsDirty); // Getting a file that wasn't in the cache is a write operation. cache.GetResXFileInfo(resx); Assert.True(cache.IsDirty); // Writing the file to disk should make the cache clean. cache.SerializeCache(stateFile, /* Log */ null); Assert.False(cache.IsDirty); // Deserialize from disk. Result should not be dirty. cache = ResGenDependencies.DeserializeCache(stateFile, true, /* Log */ null); Assert.False(cache.IsDirty); // Asking for a file that's in the cache should not dirty the cache. cache.GetResXFileInfo(resx); Assert.False(cache.IsDirty); // Changing UseSourcePath to false should dirty the cache. cache.UseSourcePath = false; Assert.True(cache.IsDirty); } finally { File.Delete(resx); File.Delete(stateFile); } }
/// <summary> /// Read the state file if able. /// </summary> private void ReadStateFile() { // First we look to see if we have a resgen linked files cache. If so, then we can use that // cache to speed up processing. If there's a problem reading the cache file (or it // just doesn't exist, then this method will return a brand new cache object. // This method eats IO Exceptions cache = ResGenDependencies.DeserializeCache((StateFile == null) ? null : StateFile.ItemSpec, UseSourcePath, Log); //RWOLFF -- throw here? //ErrorUtilities.VerifyThrow(cache != null, "We did not create a cache!"); }
/// <summary> /// Reads the .cache file from disk into a ResGenDependencies object. /// </summary> /// <param name="stateFile"></param> /// <param name="useSourcePath"></param> /// <returns></returns> internal static ResGenDependencies DeserializeCache( string stateFile, bool useSourcePath, TaskLoggingHelper log ) { ResGenDependencies retVal = (ResGenDependencies)StateFileBase.DeserializeCache( stateFile, log, typeof( ResGenDependencies ) ); if(retVal == null) { retVal = new ResGenDependencies(); } // Ensure that the cache is properly initialized with respect to how resgen will // resolve linked files within .resx files. ResGen has two different // ways for resolving relative file-paths in linked files. The way // that ResGen resolved relative paths before Whidbey was always to // resolve from the current working directory. In Whidbey a new command-line // switch "/useSourcePath" instructs ResGen to use the folder that // contains the .resx file as the path from which it should resolve // relative paths. So we should base our timestamp/existence checking // on the same switch & resolve in the same manner as ResGen. retVal.UseSourcePath = useSourcePath; return retVal; }
public void DirtyCleanScenario(bool useMSBuildResXReader) { ResGenDependencies cache = new(); string resx = CreateSampleResx(); string stateFile = FileUtilities.GetTemporaryFile(); try { // A newly created cache is not dirty. cache.IsDirty.ShouldBeFalse(); ResGenDependencies.PortableLibraryFile libFile = new("otherFileName"); libFile.outputFiles = new string[] { "first", "second" }; libFile.assemblySimpleName = "simpleName"; libFile.lastModified = DateTime.Now.Subtract(TimeSpan.FromSeconds(10)); cache.portableLibraries.Add("fileName", libFile); // Writing the file to disk should make the cache clean. cache.SerializeCache(stateFile, /* Log */ null); cache.IsDirty.ShouldBeFalse(); // Getting a file that wasn't in the cache is a write operation. cache.GetResXFileInfo(resx, useMSBuildResXReader); cache.IsDirty.ShouldBeTrue(); // Add linkedFiles to further test serialization and deserialization. cache.resXFiles.TryGetValue(resx, out ResGenDependencies.ResXFile file).ShouldBeTrue(); file.linkedFiles = new string[] { "third", "fourth" }; // Writing the file to disk should make the cache clean again. cache.SerializeCache(stateFile, /* Log */ null); cache.IsDirty.ShouldBeFalse(); // Deserialize from disk. Result should not be dirty. ResGenDependencies cache2 = ResGenDependencies.DeserializeCache(stateFile, true, /* Log */ null); cache2.IsDirty.ShouldBeFalse(); // Validate that serialization worked cache.portableLibraries.TryGetValue("fileName", out ResGenDependencies.PortableLibraryFile portableLibrary); cache2.portableLibraries.TryGetValue("fileName", out ResGenDependencies.PortableLibraryFile portableLibrary2); portableLibrary2.filename.ShouldBe(portableLibrary.filename); portableLibrary2.exists.ShouldBe(portableLibrary.exists); portableLibrary2.assemblySimpleName.ShouldBe(portableLibrary.assemblySimpleName); portableLibrary2.lastModified.ShouldBe(portableLibrary.lastModified); portableLibrary2.outputFiles.Length.ShouldBe(portableLibrary.outputFiles.Length); portableLibrary2.outputFiles[1].ShouldBe(portableLibrary.outputFiles[1]); cache.resXFiles.TryGetValue(resx, out ResGenDependencies.ResXFile resX); cache2.resXFiles.TryGetValue(resx, out ResGenDependencies.ResXFile resX2); resX2.filename.ShouldBe(resX.filename); resX2.lastModified.ShouldBe(resX.lastModified); resX2.linkedFiles.Length.ShouldBe(resX.linkedFiles.Length); resX2.linkedFiles[1].ShouldBe(resX.linkedFiles[1]); // Asking for a file that's in the cache should not dirty the cache. cache2.GetResXFileInfo(resx, useMSBuildResXReader); cache2.IsDirty.ShouldBeFalse(); // Changing UseSourcePath to false should dirty the cache. cache2.UseSourcePath = false; cache2.IsDirty.ShouldBeTrue(); } finally { File.Delete(resx); File.Delete(stateFile); } }