private async Task ExecuteCommand(string command) { if (command == "#test-color") { OutputText = GetTestColor(); return; } await SaveTextEditorContent(); await AddCommandToHistory(command); var sourceKey = GetSourceKey(); var config = await GetEnvironmentalConfig(); var fileSystem = await FileSystemProviderConnector.GetProvider(); // TODO set current folder, set encoding var engine = new ServiceEngine( configureContext: context => { context.IsAtty = config.IsAtty; context.TimeZone = config.TimeZone; context.SetEnvironmentVariables(config.EnvironmentVariables); }, createCustomProvider: mem => { mem.Attach(w => new MemoryAnsiTextWriter(w)); return(new ApplicationServiceProvider( fileSystemProviderFactory: () => fileSystem, virtualConsoleProviderFactory: () => new VirtualConsoleProvider(mem.ConsoleInput, mem.ConsoleOutput, mem.ConsoleError))); }); ServiceSession session; if (sourceKey == FileSystemRepository.InputStream.Key) { session = engine.CreateSession("-f /dev/stdin", await FileSystemRepository.GetFileContent(sourceKey)); } else { session = engine.CreateSession($"-f {(await FileSystemRepository.GetItem(sourceKey)).Path}"); } var response = session.ExecuteCommand(command); OutputText = response.OutputText; ErrorText = response.ErrorText; ResultCode = response.Status; // TODO - save possible changes in FS //if (fileSystem.HasChanges()) // await FileSystemProviderConnector.ApplyChanges(); }
public void ServiceAPI_IntegrationTests_1() { var engine = new ServiceEngine(); var session = engine.CreateSession("-f /dev/stdin", InputText); Assert.True(session.IsActive); var response = session.ExecuteCommand("bal checking --account=code"); Assert.False(response.HasErrors); Assert.Equal(BalCheckingOutputText.Replace("\r", "").Trim(), response.OutputText.Trim()); response = session.ExecuteCommand("bal checking --account=code"); Assert.False(response.HasErrors); Assert.Equal(BalCheckingOutputText.Replace("\r", "").Trim(), response.OutputText.Trim()); response = session.ExecuteCommand("bal"); Assert.False(response.HasErrors); Assert.Equal(BalOutputText.Replace("\r", "").Trim(), response.OutputText.Trim()); response = session.ExecuteCommand("reg"); Assert.False(response.HasErrors); Assert.Equal(RegOutputText.Replace("\r", "").Trim(), response.OutputText.Trim()); }
public void ServiceAPI_IntegrationTests_5() { var engine = new ServiceEngine( configureContext: context => { context.IsAtty = true; }, createCustomProvider: mem => { mem.Attach(w => new MemoryAnsiTextWriter(w)); return(null); }); var session = engine.CreateSession("-f /dev/stdin", InputText); var response = session.ExecuteCommand("bal checking --account=code"); Assert.False(response.HasErrors); Assert.Equal(BalCheckingOutputTextWithSpans.Replace("\r", "").Trim(), response.OutputText.Trim()); }
public NLedgerSession(string file) { if (!File.Exists(file)) { throw new ArgumentException($"The file {file} does not exist."); } Engine = new ServiceEngine( configureContext: context => { context.IsAtty = true; }, createCustomProvider: mem => { mem.Attach(w => new MemoryAnsiTextWriter(w)); return(null); }); Session = Engine.CreateSession(file); Initialized = true; }
public void ServiceAPI_IntegrationTests_7() { var engine = new ServiceEngine( configureContext: context => { context.SetEnvironmentVariables(new Dictionary <string, string>() { { "COLUMNS", "120" } }); }); var session = engine.CreateSession("-f /dev/stdin", InputText); var response = session.ExecuteCommand("bal checking --account=code"); Assert.False(response.HasErrors); Assert.Equal(BalCheckingOutputText.Replace("\r", "").Trim(), response.OutputText.Trim()); }
public void ServiceAPI_IntegrationTests_4() { var engine = new ServiceEngine(); var session = engine.CreateSession("-f /dev/stdin", InputText); var tasks = new List <Task <Exception> >(); for (int i = 0; i < 10; i++) { tasks.Add(Task.Run(() => CheckSessionResponseOutput(session.ExecuteCommandAsync("bal checking --account=code").Result, BalCheckingOutputText))); tasks.Add(Task.Run(() => CheckSessionResponseOutput(session.ExecuteCommandAsync("bal").Result, BalOutputText))); tasks.Add(Task.Run(() => CheckSessionResponseOutput(session.ExecuteCommandAsync("reg").Result, RegOutputText))); } Task.WhenAll(tasks).Wait(); Assert.DoesNotContain(tasks, t => t.IsFaulted); Assert.True(tasks.All(t => t.Result == null)); }
public void ServiceAPI_IntegrationTests_6() { var fs = new MemoryFileSystemProvider(); fs.CreateFile("input.dat", InputText); var engine = new ServiceEngine( createCustomProvider: mem => { return(new ApplicationServiceProvider( fileSystemProviderFactory: () => fs, virtualConsoleProviderFactory: () => new VirtualConsoleProvider(mem.ConsoleInput, mem.ConsoleOutput, mem.ConsoleError))); }); var session = engine.CreateSession("-f input.dat"); var response = session.ExecuteCommand("bal checking --account=code"); Assert.False(response.HasErrors); Assert.Equal(BalCheckingOutputText.Replace("\r", "").Trim(), response.OutputText.Trim()); }
public void CanStartNLedger() { var engine = new ServiceEngine( configureContext: context => { context.IsAtty = true; }, createCustomProvider: mem => { mem.Attach(w => new MemoryAnsiTextWriter(w)); return null; }); var session = engine.CreateSession("-f ledger.dat"); var journal = session.GlobalScope.Session.Journal; Assert.True(session.IsActive); var response = session.ExecuteCommand("bal checking --account=code"); session.ExecuteJournalAction((j) => { Xact xact = new Xact(); xact.AddPost(new Post(journal.Master, new Amount(10, new NLedger.Commodities.Commodity(session.MainApplicationContext.CommodityPool, new NLedger.Commodities.CommodityBase("$"))))); xact.AddPost(new Post(journal.Master, new Amount(-10, new NLedger.Commodities.Commodity(session.MainApplicationContext.CommodityPool, new NLedger.Commodities.CommodityBase("$"))))); j.AddXact(xact); //j.Master.AddAccount(new Account() { f}) }); // response = session.ExecuteCommand("bal checking --account=code"); //session.ExecuteCommand //j response = session.ExecuteCommand("bal"); //session.Dispose(); Assert.False(response.HasErrors); Assert.Equal(2, session.GlobalScope.Session.Journal.Xacts.Count); //session.ServiceEngine. //Assert.Equal(BalCheckingOutputText.Replace("\r", "").Trim(), response.OutputText.Trim()); }