Skip to content

Commit

Permalink
Removendo o process.exit completamente.
Browse files Browse the repository at this point in the history
Fechando a interface de leitura que segurava a execução aberta.
  • Loading branch information
leonelsanchesdasilva committed Feb 11, 2024
1 parent a4c0b3d commit e02bb7a
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 20 deletions.
2 changes: 1 addition & 1 deletion execucao.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ const principal = async () => {
}

if (opcoes.codigo) {
await delegua.executarCodigoComoArgumento(
return await delegua.executarCodigoComoArgumento(
opcoes.codigo || codigoOuNomeArquivo,
opcoes.dialeto,
Boolean(opcoes.performance)
Expand Down
6 changes: 3 additions & 3 deletions fontes/delegua.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export class Delegua implements DeleguaInterface {
): Promise<void> {
const nucleoExecucao = new NucleoExecucao(this.versao(), this.funcaoDeRetorno, this.funcaoDeRetornoMesmaLinha);
nucleoExecucao.configurarDialeto(dialeto, performance);
await nucleoExecucao.executarCodigoComoArgumento(codigo);
return await nucleoExecucao.executarCodigoComoArgumento(codigo);
}

async executarCodigoPorArquivo(
Expand All @@ -61,13 +61,13 @@ export class Delegua implements DeleguaInterface {
): Promise<any> {
const nucleoExecucao = new NucleoExecucao(this.versao(), this.funcaoDeRetorno, this.funcaoDeRetornoMesmaLinha);
nucleoExecucao.configurarDialeto(dialeto, performance);
await nucleoExecucao.carregarEExecutarArquivo(caminhoRelativoArquivo);
return await nucleoExecucao.carregarEExecutarArquivo(caminhoRelativoArquivo);
}

async iniciarLair(dialeto: string = 'delegua'): Promise<void> {
const nucleoExecucao = new NucleoExecucao(this.versao(), this.funcaoDeRetorno, this.funcaoDeRetornoMesmaLinha);
nucleoExecucao.configurarDialeto(dialeto, false);
await nucleoExecucao.iniciarLairDelegua();
return await nucleoExecucao.iniciarLairDelegua();
}

traduzirArquivo(
Expand Down
10 changes: 5 additions & 5 deletions fontes/nucleo-execucao.ts
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ export class NucleoExecucao
// Se a interface de entrada e saída ainda não está definida, definimos agora.
// A interface pode ser definida por um teste unitário antes da execução
// aqui, por exemplo.
let interfaceLeitura: any;
let interfaceLeitura: readline.Interface | any;
if (!this.interpretador.interfaceEntradaSaida) {
interfaceLeitura = readline.createInterface({
input: process.stdin,
Expand Down Expand Up @@ -410,19 +410,19 @@ export class NucleoExecucao
errosExecucao = erros;
}

if (interfaceLeitura && interfaceLeitura.hasOwnProperty("close")) {
if (interfaceLeitura && (interfaceLeitura instanceof readline.Interface || interfaceLeitura.hasOwnProperty("close"))) {
interfaceLeitura.close();
}

if (errosExecucao.length > 0) process.exit(70); // Código com exceções não tratadas
process.exit(0);
if (errosExecucao.length > 0) process.exitCode = 70; // Código com exceções não tratadas
return;
}

/**
* LAIR (Leia-Avalie-Imprima-Repita) é o modo em que Delégua executa em modo console,
* ou seja, esperando como entrada linhas de código fornecidas pelo usuário.
*/
iniciarLairDelegua(): void {
async iniciarLairDelegua(): Promise<void> {
const lexadorJson = new LexadorJson();
const formatadorJson = new FormatadorJson();

Expand Down
1 change: 1 addition & 0 deletions jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ export default async (): Promise<Config.InitialOptions> => {
preset: 'ts-jest',
testEnvironment: 'node',
coverageReporters: ['json-summary', 'lcov', 'text', 'text-summary'],
detectOpenHandles: true
};
};
11 changes: 0 additions & 11 deletions testes/nucleo-execucao.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,6 @@ describe('Núcleo de execução', () => {
const funcaoDeRetorno = (saida: string) => retornoSaida += saida;
const nucleoExecucao = new NucleoExecucao('0.1', funcaoDeRetorno);
nucleoExecucao.configurarDialeto();

// const processReal = process;
// const exitMock = jest.fn();

// Mock de `process.exit`.
// Se não for feito, o teste falha.
// @ts-ignore: `process.exit()` retorna `never`.
const mockExit = jest.spyOn(process, 'exit').mockImplementation(() => {});
// global.process = { ...processReal, exit: exitMock as any };

// Aqui vamos simular a resposta para duas variáveis de `leia()`.
const respostas = [
Expand All @@ -44,8 +35,6 @@ describe('Núcleo de execução', () => {
};
await nucleoExecucao.carregarEExecutarArquivo('./exemplos/condicionais/escolha-com-enquanto.delegua');

expect(mockExit).toHaveBeenCalledWith(0);
expect(retornoSaida.length).toBeGreaterThan(0);
// global.process = processReal;
});
});

0 comments on commit e02bb7a

Please sign in to comment.