浏览代码

Merge pull request #511 from jtpio/config-throw-no-kernel

Throw if the config does not have tmp file params
Afshin Taylor Darian 4 年之前
父节点
当前提交
a07ef51987
共有 2 个文件被更改,包括 42 次插入1 次删除
  1. 7 1
      src/config.ts
  2. 35 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}`;
   }
 

+ 35 - 0
test/config.spec.ts

@@ -0,0 +1,35 @@
+import { DebuggerConfig } from '../src/config';
+
+describe('DebuggerConfig', () => {
+  const kernel = 'python';
+  let config: DebuggerConfig;
+
+  beforeEach(() => {
+    config = new DebuggerConfig();
+  });
+
+  describe('#getCodeId', () => {
+    it('should compute a valid code id when the parameters are set', () => {
+      const [prefix, suffix] = ['foo', 'bar'];
+      config.setHashParams({ method: 'Murmur2', seed: 'bar', kernel });
+      config.setTmpFileParams({ prefix, suffix, kernel });
+      const codeId = config.getCodeId('i = 0', kernel);
+      expect(codeId.startsWith(prefix)).toBe(true);
+      expect(codeId.endsWith(suffix)).toBe(true);
+    });
+
+    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');
+    });
+  });
+});