public void Move_MoveFileToDifferentVolume() { // On Linux, /tmp is typicall a ram disk and therefore a different partition from // /var/tmp which is supposed to persist across reboots. // On Mac, /tmp isn't usually a ram disk. However, it's possible to create and mount // loop filesystems (disk images) without root privileges. So it would be possible // to extend this when porting to Mac. if (PathHelper.GetDeviceNumber("/tmp") == PathHelper.GetDeviceNumber("/var/tmp")) { Assert.Ignore("For this test /tmp and /var/tmp have to be on different partitions"); } var tempFile = Path.Combine("/tmp", Path.GetRandomFileName()); var dirOnDifferentVolume = Path.Combine("/var/tmp", Path.GetRandomFileName()); Directory.CreateDirectory(dirOnDifferentVolume); using (File.Create(tempFile)) using (TempFile.TrackExisting(tempFile)) using (TemporaryFolder.TrackExisting(dirOnDifferentVolume)) { var destinationFile = Path.Combine(dirOnDifferentVolume, "TempFile"); Assert.That(() => DirectoryHelper.Move(tempFile, destinationFile), Throws.Nothing); Assert.That(File.Exists(destinationFile), Is.True); Assert.That(File.Exists(tempFile), Is.False); } }
public void CanOverrideDefaultLocation() { string settingsPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "SIL", "SettingsProviderTests"); Directory.CreateDirectory(settingsPath); using (TemporaryFolder.TrackExisting(settingsPath)) { RegistrationSettingsProvider.SetProductName("SettingsProviderTests"); var settingsProvider = new TestCrossPlatformSettingsProvider(); settingsProvider.Initialize(null, null); // Seems to be what .NET does, despite warnings string dirPath = settingsProvider.UserConfigLocation; Assert.That(dirPath, Is.StringContaining("SettingsProviderTests")); Directory.CreateDirectory(dirPath); string filePath = Path.Combine(dirPath, TestCrossPlatformSettingsProvider.UserConfigFileName); using (new TempFile(filePath, true)) { File.WriteAllText(filePath, @"<?xml version='1.0' encoding='utf-8'?> <configuration> <userSettings> <SIL.Windows.Forms.Registration.Registration> <setting name='Email' serializeAs='String'> <value>[email protected]</value> </setting> </SIL.Windows.Forms.Registration.Registration> </userSettings> </configuration>"); Registration.Registration regSettings = Registration.Registration.Default; Assert.That(regSettings.Email, Is.EqualTo("*****@*****.**")); } } }
public void CheckForErrorsInFile_FileCorrupt_ReturnsMessage() { var settingsPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "SIL", "SettingsProviderTests"); Directory.CreateDirectory(settingsPath); using (TemporaryFolder.TrackExisting(settingsPath)) { var settingsProvider = new TestCrossPlatformSettingsProvider(); settingsProvider.Initialize(null, null); // Seems to be what .NET does, despite warnings var dirPath = settingsProvider.UserConfigLocation; Directory.CreateDirectory(dirPath); var filePath = Path.Combine(dirPath, TestCrossPlatformSettingsProvider.UserConfigFileName); using (new TempFile(filePath, true)) { File.WriteAllText(filePath, "hello world"); Assert.That(settingsProvider.CheckForErrorsInSettingsFile(), Is.Not.Null); //because we already did the check, we don't expect to see any error now using (new ErrorReport.NoNonFatalErrorReportExpected()) { var dummy = Registration.Registration.Default; } } } }
public void LoadSettings_FileCorrupt_ShowsErrorAndSelfHeals() { var settingsPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "SIL", "SettingsProviderTests"); Directory.CreateDirectory(settingsPath); using (TemporaryFolder.TrackExisting(settingsPath)) { var settingsProvider = new TestCrossPlatformSettingsProvider(); settingsProvider.Initialize(null, null); // Seems to be what .NET does, despite warnings var dirPath = settingsProvider.UserConfigLocation; Directory.CreateDirectory(dirPath); var filePath = Path.Combine(dirPath, TestCrossPlatformSettingsProvider.UserConfigFileName); File.Delete(filePath); using (new TempFile(filePath, true)) { File.WriteAllText(filePath, "hello world"); using (new ErrorReport.NonFatalErrorReportExpected()) { var dummy = Registration.Registration.Default; //by this point, we expect that the file has been fixed and we can continue normally dummy.Email = "*****@*****.**"; } } //next time, it should be healed using (new ErrorReport.NoNonFatalErrorReportExpected()) { var settingsProvider2 = new TestCrossPlatformSettingsProvider(); settingsProvider2.Initialize(null, null); // Seems to be what .NET does, despite warnings Assert.That(Registration.Registration.Default.Email, Is.EqualTo("*****@*****.**")); } } }
public void AlreadyHaveProjectFiltersOutAttemptToCloneAgain() { using (var sueRepo = new RepositoryWithFilesSetup("SueForLift", "Sue.lift", "contents")) { var fakeProjectDir = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString()); Utilities.SetProjectsPathForTests(fakeProjectDir); try { using (var tempDir = TemporaryFolder.TrackExisting(fakeProjectDir)) { var sue = sueRepo.GetRepository(); var extantDir = Path.Combine(fakeProjectDir, "extantmatchingrepo", Utilities.OtherRepositories, Utilities.LIFT); Directory.CreateDirectory(extantDir); Directory.CreateDirectory(Path.Combine(fakeProjectDir, "norepo")); sue.CloneLocalWithoutUpdate(extantDir); var strat = new LiftObtainProjectStrategy(); Assert.IsFalse(strat.ProjectFilter(sueRepo.ProjectFolder.Path)); } } finally { Utilities.SetProjectsPathForTests(null); } } }
public RepositorySetup(string cloneName, RepositorySetup sourceToClone) { Init(cloneName); string pathToProject = RootFolder.Combine(ProjectNameForTest); ProjectFolderConfig = sourceToClone.ProjectFolderConfig.Clone(); ProjectFolderConfig.FolderPath = pathToProject; sourceToClone.MakeClone(pathToProject); ProjectFolder = TemporaryFolder.TrackExisting(RootFolder.Combine(ProjectNameForTest)); var hg = new HgRepository(pathToProject, Progress); hg.SetUserNameInIni(cloneName, Progress); }
public void DoNotHaveProjectDoesNotFilterOutRepo() { using (var sueRepo = new RepositoryWithFilesSetup("Sue", SharedConstants.CustomPropertiesFilename, "contents")) { var fakeProjectDir = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString()); Directory.CreateDirectory(fakeProjectDir); using (var tempDir = TemporaryFolder.TrackExisting(fakeProjectDir)) { var extantDir = Path.Combine(fakeProjectDir, "extantmatchingrepo"); Directory.CreateDirectory(extantDir); Directory.CreateDirectory(Path.Combine(fakeProjectDir, "norepo")); var strat = new ObtainProjectStrategyFlex(); Assert.IsTrue(strat.ProjectFilter(sueRepo.ProjectFolder.Path)); } } }
public void CopyPictureFile_PersonNotYetSaved_CopiesAndRenames() { var person = Person.CreateFromName(_prj, "Beethoven"); using (var pic = new TempFile()) { person.CopyPictureFile(pic.Path); using (TemporaryFolder.TrackExisting(person.Folder)) { var expected = Path.Combine(person.Folder, "Beethoven"); expected = Path.ChangeExtension(expected, Path.GetExtension(pic.Path)); Assert.IsTrue(File.Exists(expected)); } } }
public void AlreadyHaveProjectFiltersOutAttemptToCloneAgain() { using (var sueRepo = new RepositoryWithFilesSetup("Sue", SharedConstants.CustomPropertiesFilename, "contents")) { var fakeProjectDir = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString()); using (var tempDir = TemporaryFolder.TrackExisting(fakeProjectDir)) { var sue = sueRepo.GetRepository(); var extantDir = Path.Combine(fakeProjectDir, "extantmatchingrepo"); Directory.CreateDirectory(extantDir); Directory.CreateDirectory(Path.Combine(fakeProjectDir, "norepo")); sue.CloneLocalWithoutUpdate(extantDir); var strat = new ObtainProjectStrategyFlex(); Assert.IsFalse(strat.ProjectFilter(sueRepo.ProjectFolder.Path)); } } }
private RepositoryWithFilesSetup(string userName, RepositoryWithFilesSetup cloneFromUser) { Progress = new MultiProgress(new IProgress[] { new ConsoleProgress(), _stringProgress }); RootFolder = new TemporaryFolder("ChorusTest-" + userName + "-" + Guid.NewGuid()); Console.WriteLine("TestRepository Cloned: {0}", RootFolder.Path); string pathToProject = RootFolder.Combine(Path.GetFileName(cloneFromUser.ProjectFolder.Path)); //cloneFromUser.Synchronizer.MakeClone(pathToProject, true); HgHighLevel.MakeCloneFromUsbToLocal(cloneFromUser.Repository.PathToRepo, pathToProject, Progress); ProjectFolder = TemporaryFolder.TrackExisting(RootFolder.Combine("foo project")); string pathToOurLiftFile = ProjectFolder.Combine(Path.GetFileName(cloneFromUser.UserFile.Path)); UserFile = TempFile.TrackExisting(pathToOurLiftFile); Init(userName); }
public void DoNotHaveProjectDoesNotFilterOutRepo() { using (var sueRepo = new RepositoryWithFilesSetup("SueForLift", "Sue.lift", "contents")) { var fakeProjectDir = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString()); Directory.CreateDirectory(fakeProjectDir); using (var tempDir = TemporaryFolder.TrackExisting(fakeProjectDir)) { var extantDir = Path.Combine(fakeProjectDir, "extantmatchingrepo", Utilities.OtherRepositories, Utilities.LIFT); Directory.CreateDirectory(extantDir); Directory.CreateDirectory(Path.Combine(fakeProjectDir, "norepowithoffset", Utilities.OtherRepositories, Utilities.LIFT)); Directory.CreateDirectory(Path.Combine(fakeProjectDir, "noreposansoffset")); var strat = new ObtainProjectStrategyLift(); Assert.IsTrue(strat.ProjectFilter(sueRepo.ProjectFolder.Path)); } } }
public void GetDeviceNumber_NonExistingFileInExistingSubdirectoryReturnsNumberOfParentDirectory() { if (!TmpAndRootOnDifferentPartitions) { Assert.Ignore("For this test / and /tmp have to be on different partitions"); } var dirName = Path.Combine("/tmp", Path.GetRandomFileName()); Directory.CreateDirectory(dirName); using (TemporaryFolder.TrackExisting(dirName)) { var deviceNumber = PathUtilities.GetDeviceNumber( Path.Combine(dirName, Path.GetRandomFileName())); Assert.That(deviceNumber, Is.EqualTo(PathUtilities.GetDeviceNumber("/tmp"))); } }
public void MANUAL_ReplaceFileWithUserInteractionIfNeeded_DifferentDrives_OK() { Reporting.ErrorReport.IsOkToInteractWithUser = false; using (var source = new TempFile("one")) using (var backup = new TempFile("two")) { var drives = UsbDriveInfo.GetDrives(); Assert.Greater(drives.Count, 0, "This test requires at least one writeable USB drive"); var testFolder = Path.Combine(drives[0].RootDirectory.FullName, "PalasoFileUtilsUnitTests"); Directory.CreateDirectory(testFolder); using (var folder = TemporaryFolder.TrackExisting(testFolder)) using (var destination = new TempFileFromFolder(folder)) { FileUtils.ReplaceFileWithUserInteractionIfNeeded(source.Path, destination.Path, backup.Path); } } }
public void MANUAL_ReplaceFileWithUserInteractionIfNeeded_DifferentDrives_OK() { ErrorReport.IsOkToInteractWithUser = false; using (var source = new TempFile("one")) using (var backup = new TempFile("two")) { // Since UsbDriveInfo was moved to SIL.Core.Desktop, we need to use reflection to access // the drives since we can't add a reference to SIL.Core.Desktop. Assembly coreDesktopAssembly = Assembly.Load("SIL.Core.Desktop"); Type usbDriveInfoType = coreDesktopAssembly.GetType("SIL.UsbDrive.UsbDriveInfo"); MethodInfo getDrivesMethod = usbDriveInfoType.GetMethod("GetDrives"); dynamic drives = getDrivesMethod.Invoke(usbDriveInfoType, null); Assert.Greater(drives.Count, 0, "This test requires at least one writeable USB drive"); var testFolder = Path.Combine(drives[0].RootDirectory.FullName, "PalasoFileUtilsUnitTests"); Directory.CreateDirectory(testFolder); using (var folder = TemporaryFolder.TrackExisting(testFolder)) using (var destination = new TempFileFromFolder(folder)) { FileUtils.ReplaceFileWithUserInteractionIfNeeded(source.Path, destination.Path, backup.Path); } } }
// This is a highly experimental export which may evolve as we work on this with Age of Learning. public void ExportAudioFiles1PerPage() { var container = System.IO.Path.Combine(System.IO.Path.GetTempPath(), "bloom audio export"); Directory.CreateDirectory(container); var parentFolderForAllOfTheseExports = TemporaryFolder.TrackExisting(container); var folderForThisBook = new TemporaryFolder(parentFolderForAllOfTheseExports, Path.GetFileName(this.BookSelection.CurrentSelection.FolderPath)); var pageIndex = 0; foreach (XmlElement pageElement in this.BookSelection.CurrentSelection.GetPageElements()) { ++pageIndex; //var durations = new StringBuilder(); //var accumulatedDuration = 0; try { // These elements are marked as audio-sentence but we're not sure yet if the user actually recorded them yet var audioSentenceElements = HtmlDom.SelectAudioSentenceElements(pageElement) .Cast <XmlElement>(); var mergeFiles = audioSentenceElements .Select(s => AudioProcessor.GetOrCreateCompressedAudio( this.BookSelection.CurrentSelection.FolderPath, s.Attributes["id"]?.Value)) .Where(s => !string.IsNullOrEmpty(s)); if (mergeFiles.Any()) { // enhance: it would be nice if we could somehow provide info on what should be highlighted and when, // though I don't know how that would work with Age of Learning's PDF viewer. // The following was a start on that before I realized that I don't know how that would be accomplished, // but I'm leaving it here in case I pick it up again. // foreach (var audioSentenceElement in audioSentenceElements) //{ // var id = HtmlDom.GetAttributeValue(audioSentenceElement, "id"); // var element = this.BookSelection.CurrentSelection.OurHtmlDom.SelectSingleNode($"//div[@id='{id}']"); // var duration = HtmlDom.GetAttributeValue(audioSentenceElement, "data-duration"); // Here we would need to determine the duration if data-duration is empty. // accumulatedDuration += int.Parse(duration); // durations.AppendLine(accumulatedDuration.ToString() + "\t" + duration); //} var bookName = Path.GetFileName(this.BookSelection.CurrentSelection.FolderPath); // not title, that isn't sanitized to safe characters var filename = $"{bookName}_{this._currentlyLoadedBook.BookData.Language1.Name}_{pageIndex:0000}.mp3".Replace(' ', '_'); var combinedAudioPath = Path.Combine(folderForThisBook.FolderPath, filename); var errorMessage = AudioProcessor.MergeAudioFiles(mergeFiles, combinedAudioPath); if (errorMessage != null) { File.WriteAllText(Path.Combine(folderForThisBook.FolderPath, $"error page{pageIndex}.txt"), errorMessage); } //File.WriteAllText(Path.Combine(folderForThisBook.FolderPath, $"page{pageIndex} timings.txt"), // durations.ToString()); } } catch (Exception e) { File.WriteAllText(Path.Combine(folderForThisBook.FolderPath, $"error page{pageIndex}.txt"), e.Message); } } Process.Start(folderForThisBook.FolderPath); }