private static void ValidateLanguages(Options options) { var knownLanguageCodes = KnownLanguages.AllLanguages.Select(l => l.TwoLetterIsoName); var invalidLanguages = options.Languages.Where(language => !knownLanguageCodes.Contains(language, StringComparer.OrdinalIgnoreCase)); var parserErrors = invalidLanguages.Select(language => new ParseError($"Invalid language '{language}'")); options.CustomParseErrors.AddRange(parserErrors); }
private static void HandleParseErrors(Options options) { if(options.ParseErrors.Any()) { Environment.Exit(1); } if (options.CustomParseErrors.Any()) { PrintCustomErrorInfo(options.CustomParseErrors); Environment.Exit(1); } }
private static void ValidateDownloaders(Options options) { var downloaders = ReflectionHelper.GetAllConcreteImplementors<ISubtitleDownloader>(); var knownDownloaderNames = downloaders.Select(downloader => downloader.Name.TrimSuffix("Downloader")); var invalidDownloaders = options.DownloaderNames.Where(downloader => !knownDownloaderNames.Contains(downloader, StringComparer.OrdinalIgnoreCase)); var parserErrors = invalidDownloaders.Select(downloader => new ParseError($"Invalid downloader '{downloader}'")); options.CustomParseErrors.AddRange(parserErrors); }
public void Run(Options options) { var ignoredShows = _fileOperations.GetIgnoredShows(options.IgnoreFileName); var state = _serializer.LoadState(); state.Cleanup(options.GiveupDays, _fileOperations.CreateNosrtFile); var filesToProcess = _fileOperations.GetFilesToProcess(new [] { options.Directory }, options.Languages); var failedFiles = ProcessFiles(filesToProcess, ignoredShows); state.Update(failedFiles); _serializer.SaveState(state); }
private static void HandleHelpRequests(Options options) { if (options.ListDownloaders) { CapabilitiesProvider.ListAvailableDownloaders(); Environment.Exit(0); } if (options.ListLanguages) { CapabilitiesProvider.ListAvailableLanguages(); Environment.Exit(0); } }
public void Run_Always_SavesState() { var fileSystem = A.Fake<IFileOperations>(); var serializer = A.Fake<IStateSerializer>(); var subtitleState = new SubtitleState(); A.CallTo(() => serializer.LoadState()).Returns(subtitleState); var fileProcessor = A.Fake<IFileProcessor>(); var application = new DownloaderApplication(fileSystem, serializer, fileProcessor); var options = new Options(); application.Run(options); A.CallTo(() => serializer.SaveState(subtitleState)).MustHaveHappened(); }
public Options ParseOptions(string[] args) { Options options; var results = _parser.ParseArguments<Options>(args); if (results.Tag == ParserResultType.Parsed) { options = ((Parsed<Options>)results).Value; ValidateLanguages(options); ValidateDownloaders(options); return options; } options = new Options(); var errors = ((NotParsed<Options>)results).Errors; var parseErrors = errors.Select(e => new ParseError(e.Tag.ToString())); options.ParseErrors.AddRange(parseErrors); return options; }
public void Run_IfFilesFail_AddsThemToState() { var files = new[] {"fail1.avi", "success.avi", "fail2.avi"}; var fileSystem = A.Fake<IFileOperations>(); A.CallTo(() => fileSystem.GetFilesToProcess(A<IEnumerable<string>>._, A<IEnumerable<string>>._)).Returns(files); var serializer = A.Fake<IStateSerializer>(); var subtitleState = new SubtitleState(); A.CallTo(() => serializer.LoadState()).Returns(subtitleState); var fileProcessor = A.Fake<IFileProcessor>(); A.CallTo(() => fileProcessor.ProcessFile(A<string>.That.StartsWith("fail"), A<IEnumerable<string>>._)).Returns(false); A.CallTo(() => fileProcessor.ProcessFile(A<string>.That.StartsWith("success"), A<IEnumerable<string>>._)).Returns(true); var application = new DownloaderApplication(fileSystem, serializer, fileProcessor); var options = new Options { GiveupDays = 99 }; application.Run(options); Assert.That(subtitleState.Entries.Select(s => s.File), Is.EquivalentTo(new []{ "fail1.avi", "fail2.avi"})); }
public void Run_FilesAreCleanedUp_CleanedFilesGetsNosrtWritten() { var fileSystem = A.Fake<IFileOperations>(); var serializer = A.Fake<IStateSerializer>(); var subtitleState = new SubtitleState(); subtitleState.Entries.Add(new SubtitleStateEntry("cleaned1", DateTime.Now.AddDays(-100))); subtitleState.Entries.Add(new SubtitleStateEntry("cleaned2", DateTime.Now.AddDays(-101))); subtitleState.Entries.Add(new SubtitleStateEntry("stays", DateTime.Now.AddDays(-1))); A.CallTo(() => serializer.LoadState()).Returns(subtitleState); var fileProcessor = A.Fake<IFileProcessor>(); var application = new DownloaderApplication(fileSystem, serializer, fileProcessor); var options = new Options {GiveupDays = 99}; application.Run(options); A.CallTo(() => fileSystem.CreateNosrtFile(A<SubtitleStateEntry>.That.Matches(s => s.File.StartsWith("cleaned")))).MustHaveHappened(Repeated.Exactly.Times(2)); A.CallTo(() => fileSystem.CreateNosrtFile(A<SubtitleStateEntry>.That.Matches(s => s.File.StartsWith("stays")))).MustNotHaveHappened(); }
private static DownloaderApplication InitializeApplication(Options options) { var container = new Container(x => x.AddRegistry(new SubtitleDownloaderRegistry(options))); var application = container.GetInstance<DownloaderApplication>(); return application; }