Skip to content
This repository has been archived by the owner on Feb 16, 2024. It is now read-only.

Commit

Permalink
finalize
Browse files Browse the repository at this point in the history
  • Loading branch information
DmytroHryshyn committed Jan 31, 2024
1 parent 96cd2f9 commit 7c628f9
Show file tree
Hide file tree
Showing 8 changed files with 111 additions and 109 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -308,4 +308,4 @@
"extensionDependencies": [
"vscode.git"
]
}
}
2 changes: 1 addition & 1 deletion src/axios/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ const client = axios.create();
axiosRetry(client);

export const DEFAULT_RETRY_COUNT = 3;
export default client;
export default client;
21 changes: 10 additions & 11 deletions src/components/bootstrapExecutablesService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export class BootstrapExecutablesService {
private readonly __globalStorageUri: Uri,
private readonly __fileSystem: FileSystem,
private readonly __messageBus: MessageBus,
private readonly __telemetryService: Telemetry
private readonly __telemetryService: Telemetry,
) {
__messageBus.subscribe(MessageKind.bootstrapEngine, () =>
this.__onBootstrapEngines(),
Expand All @@ -25,17 +25,16 @@ export class BootstrapExecutablesService {
const codemodEngineNodeExecutableUri =
await this.__bootstrapCodemodEngineNodeExecutableUri();

// Uri.file('/intuita/codemod-engine-rust/target/release/codemod-engine-rust');
const codemodEngineRustExecutableUri =
await this.__bootstrapCodemodEngineRustExecutableUri();
// Uri.file('/intuita/codemod-engine-rust/target/release/codemod-engine-rust');
const codemodEngineRustExecutableUri =
await this.__bootstrapCodemodEngineRustExecutableUri();

this.__messageBus.publish({
kind: MessageKind.engineBootstrapped,
codemodEngineNodeExecutableUri,
codemodEngineRustExecutableUri,
});

} catch(e) {
this.__messageBus.publish({
kind: MessageKind.engineBootstrapped,
codemodEngineNodeExecutableUri,
codemodEngineRustExecutableUri,
});
} catch (e) {
const message = e instanceof Error ? e.message : String(e);

window.showErrorMessage(message);
Expand Down
18 changes: 9 additions & 9 deletions src/components/downloadService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ export class DownloadService {
let response;

try {
response = await axios.head(url, {
timeout: 15000,
response = await axios.head(url, {
timeout: 15000,
'axios-retry': {
retries: DEFAULT_RETRY_COUNT
}
retries: DEFAULT_RETRY_COUNT,
},
});
} catch (error) {
if (localModificationTime > 0) {
Expand Down Expand Up @@ -75,16 +75,16 @@ export class DownloadService {
uri: Uri,
chmod: Mode | null,
): Promise<void> {
const response = await axios.get(url, {
responseType: 'arraybuffer',
const response = await axios.get(url, {
responseType: 'arraybuffer',
'axios-retry': {
retries: DEFAULT_RETRY_COUNT
}
retries: DEFAULT_RETRY_COUNT,
},
});
const content = new Uint8Array(response.data);

await this.#fileSystem.writeFile(uri, content);

if (chmod !== null) {
await this.#fileSystemUtilities.setChmod(uri, chmod);
}
Expand Down
40 changes: 21 additions & 19 deletions src/data/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,28 +39,30 @@ const deserializeState = (serializedState: string) => {
};

const getPreloadedState = async (storage: MementoStorage) => {
const initialState =
(await storage.getItem(`${PERSISTANCE_PREFIX}:${PERSISTANCE_KEY}`));
const initialState = await storage.getItem(
`${PERSISTANCE_PREFIX}:${PERSISTANCE_KEY}`,
);

if(initialState === null) {
return null;
}
if (!initialState) {
return null;
}

const deserializedState = deserializeState(initialState);
const deserializedState = deserializeState(initialState);

if (!deserializedState) {
return null;
}

if(deserializeState === null) {
return null;
}
const decodedState = persistedStateCodecNew.decode(deserializedState);

const decodedState = persistedStateCodecNew.decode(deserializedState);
// should never happen because of codec fallback
if (decodedState._tag !== 'Right') {
return null;
}

// should never happen because of codec fallback
if (decodedState._tag !== 'Right') {
throw new Error('Invalid state');
}
return decodedState.right;
};

return decodedState.right;
}
const buildStore = async (workspaceState: Memento) => {
const storage = new MementoStorage(workspaceState);

Expand Down Expand Up @@ -93,10 +95,10 @@ const buildStore = async (workspaceState: Memento) => {

const preloadedState = await getPreloadedState(storage);

if(preloadedState === null) {
window.showWarningMessage('Unable to get preloaded state');
if (preloadedState === null) {
window.showWarningMessage('Unable to get preloaded state.');
}

const store = configureStore({
reducer: validatedReducer,
...(preloadedState !== null && { preloadedState }),
Expand Down
4 changes: 1 addition & 3 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,14 +140,12 @@ export async function activate(context: vscode.ExtensionContext) {
context.globalStorageUri,
vscode.workspace.fs,
messageBus,
vscodeTelemetry
vscodeTelemetry,
);

const intuitaTextDocumentContentProvider =
new IntuitaTextDocumentContentProvider();



const mainViewProvider = new MainViewProvider(
context,
userService,
Expand Down
17 changes: 9 additions & 8 deletions src/telemetry/telemetry.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import type { CaseHash } from '../cases/types';

export type ErrorEvent = Readonly<{
kind: 'failedToExecuteCommand';
commandName: string;
}> |
Readonly<{
kind: 'failedToBootstrapEngines',
message: string;
}>
export type ErrorEvent =
| Readonly<{
kind: 'failedToExecuteCommand';
commandName: string;
}>
| Readonly<{
kind: 'failedToBootstrapEngines';
message: string;
}>;

export type Event =
| Readonly<{
Expand Down
116 changes: 59 additions & 57 deletions test/dowloadService.test.ts
Original file line number Diff line number Diff line change
@@ -1,81 +1,83 @@
import { describe, test, afterEach, vi, expect } from 'vitest';
import { describe, test, afterEach, vi, expect } from 'vitest';
import { DownloadService } from '../src/components/downloadService';
import type { FileSystem } from 'vscode';
import { AxiosError, AxiosInstance } from 'axios';
import { AxiosError, AxiosInstance } from 'axios';
import nock from 'nock';
import axiosInstance from '../src/axios';

const mockedFileSystemUtilities = {
getModificationTime: vi.fn(() => 1),
setChmod: vi.fn()
const mockedFileSystemUtilities = {
getModificationTime: vi.fn(() => 1),
setChmod: vi.fn(),
};
const fs = {
writeFile: vi.fn(),
stat: vi.fn(() => ({ mtime: Date.now() }))
} as unknown as FileSystem;

const fs = {
writeFile: vi.fn(),
stat: vi.fn(() => ({ mtime: Date.now() })),
} as unknown as FileSystem;

const downloadService = new DownloadService(
fs,
// @ts-ignore
mockedFileSystemUtilities,
fs,
// @ts-ignore
mockedFileSystemUtilities,
);

const NETWORK_ERROR = new AxiosError('Some connection error');
NETWORK_ERROR.code = 'ECONNRESET';

// 3 failed responses, then good response
const responses = [
() => nock('https://test.com').head('/test').replyWithError(NETWORK_ERROR),
() => nock('https://test.com').head('/test').replyWithError(NETWORK_ERROR),
() => nock('https://test.com').head('/test').replyWithError(NETWORK_ERROR),
() => nock('https://test.com').head('/test')
.reply(200, '', { 'last-modified': new Date(2).toISOString() }),
() => nock('https://test.com').get('/test').replyWithError(NETWORK_ERROR),
() => nock('https://test.com').get('/test').replyWithError(NETWORK_ERROR),
() => nock('https://test.com').get('/test').replyWithError(NETWORK_ERROR),
() => nock('https://test.com').get('/test')
.reply(200, 'Test'),
() => nock('https://test.com').head('/test').replyWithError(NETWORK_ERROR),
() => nock('https://test.com').head('/test').replyWithError(NETWORK_ERROR),
() => nock('https://test.com').head('/test').replyWithError(NETWORK_ERROR),
() =>
nock('https://test.com')
.head('/test')
.reply(200, '', { 'last-modified': new Date(2).toISOString() }),
() => nock('https://test.com').get('/test').replyWithError(NETWORK_ERROR),
() => nock('https://test.com').get('/test').replyWithError(NETWORK_ERROR),
() => nock('https://test.com').get('/test').replyWithError(NETWORK_ERROR),
() => nock('https://test.com').get('/test').reply(200, 'Test'),
];

const setupResponses = (client: AxiosInstance, responses: Array<Function>) => {
const configureResponse = () => {
const response = responses.shift();
if (response) {
response();
}
};

const configureResponse = () => {
const response = responses.shift();
if (response) {
response();
}
};

client.interceptors.request.use(
(config) => {
configureResponse();
return config;
},
(error) => {
configureResponse();
return Promise.reject(error);
},
);
}
client.interceptors.request.use(
(config) => {
configureResponse();
return config;
},
(error) => {
configureResponse();
return Promise.reject(error);
},
);
};

describe('DownloadService', () => {
afterEach(() => {
nock.cleanAll();
nock.enableNetConnect();
});
afterEach(() => {
nock.cleanAll();
nock.enableNetConnect();
});

test('Should retry 3 times if request fails', async () => {
test('Should retry 3 times if request fails', async () => {
setupResponses(axiosInstance, responses);

setupResponses(axiosInstance, responses);

await downloadService.downloadFileIfNeeded(
`https://test.com/test`,
// @ts-expect-error passing a string instead of URI, because URI cannot be imported from vscode
'/',
'755',
);
await downloadService.downloadFileIfNeeded(
`https://test.com/test`,
// @ts-expect-error passing a string instead of URI, because URI cannot be imported from vscode
'/',
'755',
);

expect(fs.writeFile).toBeCalledWith('/', new Uint8Array( [84, 101, 115, 116]))
});
})
expect(fs.writeFile).toBeCalledWith(
'/',
new Uint8Array([84, 101, 115, 116]),
);
});
});

0 comments on commit 7c628f9

Please sign in to comment.