Procházet zdrojové kódy

Throw if the kernel does not have tmp file params

Jeremy Tuloup před 4 roky
rodič
revize
11f761e4be
2 změnil soubory, kde provedl 33 přidání a 1 odebrání
  1. 7 1
      src/config.ts
  2. 26 0
      test/config.spec.ts

+ 7 - 1
src/config.ts

@@ -16,13 +16,19 @@ export class DebuggerConfig implements IDebugger.IConfig {
    * @param kernel The kernel name from current session.
    */
   getCodeId(code: string, kernel: string): string {
-    const { prefix, suffix } = this._fileParams.get(kernel);
+    const fileParams = this._fileParams.get(kernel);
+
+    if (!fileParams) {
+      throw new Error(`Kernel (${kernel}) has no tmp file params.`);
+    }
+
     const hash = this._hashMethods.get(kernel);
 
     if (!hash) {
       throw new Error(`Kernel (${kernel}) has no hashing params.`);
     }
 
+    const { prefix, suffix } = fileParams;
     return `${prefix}${hash(code)}${suffix}`;
   }
 

+ 26 - 0
test/config.spec.ts

@@ -0,0 +1,26 @@
+import { DebuggerConfig } from '../src/config';
+
+describe('DebuggerConfig', () => {
+  const kernel = 'python';
+  let config: DebuggerConfig;
+
+  beforeEach(() => {
+    config = new DebuggerConfig();
+  });
+
+  describe('#getCodeId', () => {
+    it('should throw if the kernel does not have hash parameters', () => {
+      config.setTmpFileParams({ prefix: 'foo', suffix: 'bar', kernel });
+      expect(() => {
+        config.getCodeId('i = 0', kernel);
+      }).toThrow('has no hashing params');
+    });
+
+    it('should throw if the kernel does not have tmp file parameters', () => {
+      config.setHashParams({ method: 'Murmur2', seed: 'bar', kernel });
+      expect(() => {
+        config.getCodeId('i = 0', kernel);
+      }).toThrow('has no tmp file params');
+    });
+  });
+});