浏览代码

Throw if the kernel does not have tmp file params

Jeremy Tuloup 4 年之前
父节点
当前提交
11f761e4be
共有 2 个文件被更改,包括 33 次插入1 次删除
  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');
+    });
+  });
+});