public string ReadFile(string path) { var segs = path.Split(new[] { this.DirectorySeparatorChar }, StringSplitOptions.RemoveEmptyEntries); FakeDirectory d = Traverse(segs.Take(segs.Length - 1)); return(((FakeFile)d.Entries[segs.Last()]).Contents); }
public void GetInstantaneousFlowData() { TimeRunner.Run(async(time, th) => { var entries = new Dictionary <(string uri, string soapAction, string soapBody), XElement> { { ("http://energywatch.natgrid.co.uk/EDP-PublicUI/PublicPI/InstantaneousFlowWebService.asmx", "http://www.NationalGrid.com/EDP/UI/GetInstantaneousFlowData", "<GetInstantaneousFlowData xmlns=\"http://www.NationalGrid.com/EDP/UI/\" />"), Soap.LoadBody("GetInstantaneousFlowData1") } }; var fakeSoapDownloader = new FakeSoapDownloader(entries); var flow = new InstantaneousFlow(th, fakeSoapDownloader); var stringsDir = new FakeDirectory(th); var strings = new Strings(th, stringsDir); var data = await(await flow.GetInstantaneousFlowDataAsync().ConfigureAwait(th)).WriteStringsAsync(th, strings).ConfigureAwait(th); Assert.Equal(258, data.Count); var nameIndex0 = await strings.AddOrGetIndexAsync("ALDBROUGH").ConfigureAwait(th); Assert.Equal(new InstantaneousFlow.Data(Instant.FromUtc(2017, 9, 15, 6, 24), InstantaneousFlow.SupplyType.ZoneSupply, (ushort)nameIndex0, Flow.Zero), data.First()); var nameIndex1 = await strings.AddOrGetIndexAsync("TOTAL SUPPLY").ConfigureAwait(th); Assert.Equal(new InstantaneousFlow.Data(Instant.FromUtc(2017, 9, 15, 6, 34), InstantaneousFlow.SupplyType.TotalSupply, (ushort)nameIndex1, Flow.FromCubicMetersPerHour(190.03944 * 1e6 / 24)), data.Last()); }); }
public void Fetch() { TimeRunner.Run(async(time, th) => { var downloader = new FakeElexonDownloader(); var dir = new FakeDirectory(th); var fetcher = new FetchB1610(th, downloader, dir, time, null); var cts = new CancellationTokenSource(); Task unused = th.Run(async() => { await th.Delay(NodaTime.Duration.FromMinutes(5)).ConfigureAwait(th); cts.Cancel(); }); await fetcher.Start(cts.Token).ConfigureAwaitHideCancel(th); var reader = new B1610.Reader(th, dir); Assert.Equal(100 + 103, await reader.CountAsync().ConfigureAwait(th)); Assert.Equal(new B1610.Data("GRAI-6", Instant.FromUtc(2017, 7, 31, 23, 0), Power.FromKilowatts(226_200)), await(await reader.ReadAsync(0, 1)).First()); Assert.Equal(new B1610.Data("GRGBW-1", Instant.FromUtc(2017, 7, 31, 23, 30), Power.FromKilowatts(43_390)), await(await reader.ReadAsync(100 + 103 - 1, 100 + 103)).First()); }, startInstant: NodaTime.Instant.FromUtc(2017, 8, 10, 22, 30)); }
public void DeleteDirectory(string path) { var segs = path.Split(new[] { this.DirectorySeparatorChar }, StringSplitOptions.RemoveEmptyEntries); FakeDirectory d = Traverse(segs.Take(segs.Length - 1)); d.Entries.Remove(segs.Last()); }
public FakeDirectory FindDirectory(DirectoryPath path) { // Want the root? if (path.Segments.Length == 0) { return(_root); } var queue = new Queue <string>(path.Segments); FakeDirectory current = null; var children = _root.Content; while (queue.Count > 0) { // Set the parent. var parent = current; // Calculate the current path. var segment = queue.Dequeue(); path = parent != null?parent.Path.Combine(segment) : new DirectoryPath(segment); // Find the current path. if (!children.Directories.ContainsKey(path)) { return(null); } current = children.Directories[path]; children = current.Content; } return(current); }
public void Fetch() { TimeRunner.Run(async(time, th) => { var downloader = new FakeElexonDownloader(); var dir = new FakeDirectory(th); var fetcher = new FetchPhyBmData(th, downloader, dir, time); var cts = new CancellationTokenSource(); Task unused = th.Run(async() => { await th.Delay(NodaTime.Duration.FromMinutes(60)).ConfigureAwait(th); cts.Cancel(); }); await fetcher.Start(false, cts.Token).ConfigureAwaitHideCancel(th); var reader = new PhyBmData.FpnReader(th, dir); Assert.Equal(1031 + 1023, await reader.CountAsync().ConfigureAwait(th)); var cldcwIdHash = EicIds.Hash("CLDCW-1"); var cldcwTask = (await reader.ReadAsync().ConfigureAwait(th)).Where(x => x.ResourceNameHash == cldcwIdHash); var cldcw = await cldcwTask.ToList().ConfigureAwait(th); Assert.Equal(4, cldcw.Count); Assert.Equal(new [] { 13.0, 11.0, 11.0, 9.0 }, cldcw.Select(x => x.LevelTo.Megawatts)); }, startInstant: NodaTime.Instant.FromUtc(2017, 8, 5, 22, 29)); }
public void WriteFile(string path, string contents) { var segs = path.Split(new[] { this.DirectorySeparatorChar }, StringSplitOptions.RemoveEmptyEntries); FakeDirectory d = Traverse(segs.Take(segs.Length - 1)); ((FakeFile)d.Entries[segs.Last()]).Contents = contents; }
/// <summary> /// Initializes a new instance of the <see cref="PaketPackerFixture" /> class with the directory output = NuGet. /// </summary> internal PaketPackerFixture() : base("paket.exe") { FakeDirectory fakeDirectory = FileSystem.CreateDirectory("NuGet"); Output = fakeDirectory.Path; FakeArguments = Substitute.For <ICakeArguments>(); }
public TextReader CreateStreamReader(string path) { var segs = path.Split(new[] { this.DirectorySeparatorChar }, StringSplitOptions.RemoveEmptyEntries); FakeDirectory d = Traverse(segs.Take(segs.Length - 1)); Debug.Assert(d != null, path); Debug.Assert(d.Entries != null, "d.entries"); Debug.Assert(segs != null, "segs"); return(new StringReader(((FakeFile)d.Entries[segs.Last()]).Contents)); }
public FakeFileSystem Dir(string name) { var d = new FakeDirectory { Parent = dir }; dir.Entries[name] = d; dir = d; return(this); }
public IDirectory Create(FullPath name) { FakeDirectory dir; if (!this.fakeDirs.TryGetValue(name, out dir)) { dir = new FakeDirectory(name); this.fakeDirs.Add(name, dir); } return(dir); }
public void DeleteDirectory(FakeDirectory fakeDirectory, bool recursive) { var root = new Stack <FakeDirectory>(); var result = new Stack <FakeDirectory>(); if (fakeDirectory.Exists) { root.Push(fakeDirectory); } while (root.Count > 0) { var node = root.Pop(); result.Push(node); var directories = node.Content.Directories; if (directories.Count > 0 && !recursive) { throw new IOException("The directory is not empty."); } foreach (var child in directories) { root.Push(child.Value); } } while (result.Count > 0) { var directory = result.Pop(); var files = directory.Content.Files.Select(x => x).ToArray(); if (files.Length > 0 && !recursive) { throw new IOException("The directory is not empty."); } foreach (var file in files) { // Delete the file. DeleteFile(file.Value); } // Delete the directory. directory.Parent.Content.Remove(directory); directory.Exists = false; } }
public static Mock <IFileProvider> SetupFileProviderMock(this Mock <IFileProvider> fileProviderMock, string root, List <FakeDirectory> directories) { var rootDirectory = directories.SingleOrDefault(x => x.Name == string.Empty); if (rootDirectory != null) { directories.Remove(rootDirectory); } else { rootDirectory = new FakeDirectory(string.Empty, new FakeFile[] { }); } ProcessDirectory(fileProviderMock, root, rootDirectory, directories); return(fileProviderMock); }
public bool FileExists(string path) { var segs = path.Split(new[] { this.DirectorySeparatorChar }, StringSplitOptions.RemoveEmptyEntries); FakeDirectory d = Traverse(segs.Take(segs.Length - 1)); if (d == null) { return(false); } Entry e; if (!d.Entries.TryGetValue(segs.Last(), out e)) { return(false); } return(e is FakeFile); }
public void AppendLines() { TimeRunner.Run(async(time, th) => { var nl = Environment.NewLine; var expected = $"one{nl}two{nl}"; var dir = new FakeDirectory(th); var appender = new RollingFileAppender(dir, "abc", th); await appender.AppendLineAsync("one").ConfigureAwait(th); await appender.AppendLineAsync("two").ConfigureAwait(th); var files = (await dir.ListFilesAsync().ConfigureAwait(th)).ToList(); Assert.Single(files); Assert.Equal(expected.Length, files[0].Length); var stream = await dir.ReadAsync(files[0].Id).ConfigureAwait(th); var ms = new MemoryStream(); await stream.CopyToAsync(ms).ConfigureAwait(th); Assert.Equal(expected, Encoding.UTF8.GetString(ms.ToArray())); }); }
public void StoreAndLoad() { TimeRunner.Run(async(time, th) => { var dir = new FakeDirectory(th); var fuelInstHhCur = new FuelInstHhCur(th, new FakeElexonDownloader()); var data = await fuelInstHhCur.GetAsync().ConfigureAwait(th); var writer = new FuelInstHhCur.Writer(th, dir); var reader = new FuelInstHhCur.Reader(th, dir); Assert.Equal(0, await reader.CountAsync().ConfigureAwait(th)); Assert.Equal(0, await(await reader.ReadAsync().ConfigureAwait(th)).Count().ConfigureAwait(th)); await writer.AppendAsync(data).ConfigureAwait(th); Assert.Equal(1, await reader.CountAsync().ConfigureAwait(th)); Assert.Equal(1, await(await reader.ReadAsync().ConfigureAwait(th)).Count().ConfigureAwait(th)); var data1 = await(await reader.ReadAsync().ConfigureAwait(th)).Last().ConfigureAwait(th); Assert.Equal(data, data1); var data2 = await(await reader.ReadAsync(0, 1).ConfigureAwait(th)).Single().ConfigureAwait(th); Assert.Equal(data, data2); }); }
public void StoreAndLoad() { TimeRunner.Run(async(time, th) => { var dir = new FakeDirectory(th); var phy = new PhyBmData(th, new FakeElexonDownloader()); var data = await phy.GetAsync(new LocalDate(2017, 8, 6), 1).ConfigureAwait(th); var writer = new PhyBmData.FpnWriter(th, dir); var reader = new PhyBmData.FpnReader(th, dir); Assert.Equal(0, await reader.CountAsync().ConfigureAwait(th)); Assert.Equal(0, await(await reader.ReadAsync().ConfigureAwait(th)).Count().ConfigureAwait(th)); await writer.AppendAsync(data).ConfigureAwait(th); Assert.Equal(1031, await reader.CountAsync().ConfigureAwait(th)); Assert.Equal(data[0], await(await reader.ReadAsync(0, 1).ConfigureAwait(th)).Single().ConfigureAwait(th)); Assert.Equal(data[10], await(await reader.ReadAsync(10, 11).ConfigureAwait(th)).Single().ConfigureAwait(th)); Assert.Equal(data.Take(10), await(await reader.ReadAsync(0, 10).ConfigureAwait(th)).ToArray().ConfigureAwait(th)); Assert.Equal(data.Skip(50).Take(10), await(await reader.ReadAsync(50, 60).ConfigureAwait(th)).ToArray().ConfigureAwait(th)); Assert.Equal(data, await(await reader.ReadAsync().ConfigureAwait(th)).ToList().ConfigureAwait(th)); }); }
public void StoreAndLoad() { TimeRunner.Run(async(time, th) => { var dir = new FakeDirectory(th); var freq = new Freq(th, new FakeElexonDownloader()); var data = (await freq.GetAsync(Instant.FromUtc(2017, 8, 2, 10, 30), Instant.FromUtc(2017, 8, 2, 11, 30)).ConfigureAwait(th)); var writer = new Freq.Writer(th, dir); var reader = new Freq.Reader(th, dir); Assert.Equal(0, await reader.CountAsync().ConfigureAwait(th)); Assert.Equal(0, await(await reader.ReadAsync().ConfigureAwait(th)).Count().ConfigureAwait(th)); await writer.AppendAsync(data).ConfigureAwait(th); Assert.Equal(60 * 4 + 1, await reader.CountAsync().ConfigureAwait(th)); Assert.Equal(data[0], await(await reader.ReadAsync(0, 1).ConfigureAwait(th)).Single().ConfigureAwait(th)); Assert.Equal(data[10], await(await reader.ReadAsync(10, 11).ConfigureAwait(th)).Single().ConfigureAwait(th)); Assert.Equal(data.Take(10), await(await reader.ReadAsync(0, 10).ConfigureAwait(th)).ToArray().ConfigureAwait(th)); Assert.Equal(data.Skip(50).Take(10), await(await reader.ReadAsync(50, 60).ConfigureAwait(th)).ToArray().ConfigureAwait(th)); Assert.Equal(data, await(await reader.ReadAsync().ConfigureAwait(th)).ToList().ConfigureAwait(th)); }); }
public FakeFileSystemTree(ICakeEnvironment environment) { if (environment == null) { throw new ArgumentNullException(nameof(environment)); } if (environment.WorkingDirectory == null) { throw new ArgumentException("Working directory not set."); } if (environment.WorkingDirectory.IsRelative) { throw new ArgumentException("Working directory cannot be relative."); } Comparer = new PathComparer(environment.Platform.IsUnix()); _root = new FakeDirectory(this, "/") { Exists = true, LastWriteTime = DateTime.Now }; _root.Create(); }
public void CreateDirectory(string path) { var segs = path.Split(new [] {this.DirectorySeparatorChar}, StringSplitOptions.RemoveEmptyEntries); var d = root; foreach (var seg in segs) { Entry e; if (!d.Entries.TryGetValue(seg, out e)) { var dNew = new FakeDirectory { Parent = d }; d.Entries.Add(seg, dNew); d = dNew; } else { d = (FakeDirectory) e; } } }
public void CreateDirectory(string path) { var segs = path.Split(new [] { this.DirectorySeparatorChar }, StringSplitOptions.RemoveEmptyEntries); var d = root; foreach (var seg in segs) { Entry e; if (!d.Entries.TryGetValue(seg, out e)) { var dNew = new FakeDirectory { Parent = d }; d.Entries.Add(seg, dNew); d = dNew; } else { d = (FakeDirectory)e; } } }
public void Fetch() { TimeRunner.Run(async(time, th) => { var downloader = new FakeElexonDownloader(); var dir = new FakeDirectory(th); var fetcher = new FetchFreq(th, downloader, dir, time); var cts = new CancellationTokenSource(); Task unused = th.Run(async() => { await th.Delay(NodaTime.Duration.FromMinutes(3)).ConfigureAwait(th); cts.Cancel(); }); await fetcher.Start(false, cts.Token).ConfigureAwaitHideCancel(th); var reader = new Freq.Reader(th, dir); Assert.Equal((60 + 2) * 4, await reader.CountAsync().ConfigureAwait(th)); var data = await(await reader.ReadAsync().ConfigureAwait(th)).ToList().ConfigureAwait(th); Assert.Equal((60 + 2) * 4, data.Count); Assert.Equal(new Freq.Data(NodaTime.Instant.FromUtc(2017, 8, 2, 11, 0, 15), Frequency.FromHertz(49.910)), data.First()); Assert.Equal(new Freq.Data(NodaTime.Instant.FromUtc(2017, 8, 2, 12, 2, 0), Frequency.FromHertz(50.040)), data.Last()); }, startInstant: NodaTime.Instant.FromUtc(2017, 8, 2, 12, 0)); }
public FakeDirectory CreateDirectory(FakeDirectory directory) { var path = directory.Path; var queue = new Queue <string>(path.Segments); FakeDirectory current = null; var children = _root.Content; while (queue.Count > 0) { // Get the segment. var currentSegment = queue.Dequeue(); var parent = current; // Calculate the current path. path = parent != null?parent.Path.Combine(currentSegment) : new DirectoryPath(currentSegment); if (!children.Directories.ContainsKey(path)) { current = queue.Count == 0 ? directory : new FakeDirectory(this, path); current.Parent = parent ?? _root; current.Hidden = false; children.Add(current); } else { current = children.Directories[path]; } current.Exists = true; current.LastWriteTime = DateTime.Now; children = current.Content; } return(directory); }
/// <summary> /// Hides the specified directory. /// </summary> /// <param name="directory">The directory to hide.</param> /// <returns>The same <see cref="FakeDirectory"/> instance so that multiple calls can be chained.</returns> public static FakeDirectory Hide(this FakeDirectory directory) { directory.Hidden = true; return(directory); }
private static void ProcessDirectory(Mock <IFileProvider> fileProviderMock, string root, FakeDirectory current, List <FakeDirectory> directories) { var subDirectories = directories.Where(x => x.FolderPath.Equals(current.Name)).ToList(); directories = directories.Except(subDirectories).ToList(); var fileInfos = new List <IFileInfo>(); foreach (var file in current.Files) { var fileInfo = fileProviderMock.CreateFile(root, file.FilePath, file.Bytes); fileInfos.Add(fileInfo); } foreach (var directory in subDirectories) { var directoryInfo = fileProviderMock.CreateDirectory(root, directory.Name); fileInfos.Add(directoryInfo); ProcessDirectory(fileProviderMock, root, directory, directories); } var directoryContents = fileInfos.CreateMock <IDirectoryContents, IFileInfo>(); directoryContents.Setup(x => x.Exists).Returns(true); fileProviderMock .Setup(x => x.GetDirectoryContents(It.Is <string>(p => p.Equals(current.Name)))) .Returns(directoryContents.Object); }
public FakeFileSystem Dir(string name) { var d = new FakeDirectory { Parent = dir }; dir.Entries[name] = d; dir = d; return this; }
public FakeFileSystem End() { dir = dir.Parent; return this; }
public FakeFileSystem() { this.root = new FakeDirectory(); this.dir = root; }
public FakeFileSystem End() { dir = dir.Parent; return(this); }
public void MoveDirectory(FakeDirectory fakeDirectory, DirectoryPath destination) { var root = new Stack <FakeDirectory>(); var result = new Stack <FakeDirectory>(); if (string.IsNullOrEmpty(destination.FullPath)) { throw new ArgumentException("The destination directory is empty."); } if (fakeDirectory.Path.Equals(destination)) { throw new IOException("The directory being moved and the destination directory have the same name."); } if (FindDirectory(destination) != null) { throw new IOException("The destination directory already exists."); } string destinationParentPathStr = string.Join("/", destination.Segments.Take(destination.Segments.Length - 1).DefaultIfEmpty("/")); DirectoryPath destinationParentPath = new DirectoryPath(destinationParentPathStr == string.Empty ? "/" : destinationParentPathStr); if (FindDirectory(destinationParentPath) == null) { throw new DirectoryNotFoundException("The parent destination directory " + destinationParentPath.FullPath + " could not be found."); } if (fakeDirectory.Exists) { root.Push(fakeDirectory); } // Create destination directories and move files while (root.Count > 0) { var node = root.Pop(); result.Push(node); // Create destination directory DirectoryPath relativePath = fakeDirectory.Path.GetRelativePath(node.Path); DirectoryPath destinationPath = destination.Combine(relativePath); CreateDirectory(destinationPath); var files = node.Content.Files.Select(x => x).ToArray(); foreach (var file in files) { // Move the file. MoveFile(file.Value, destinationPath.CombineWithFilePath(file.Key.GetFilename())); } var directories = node.Content.Directories; foreach (var child in directories) { root.Push(child.Value); } } // Delete source directories while (result.Count > 0) { var directory = result.Pop(); // Delete the directory. directory.Parent.Content.Remove(directory); directory.Exists = false; } }