Skip to content

Commit 2f340ad

Browse files
committed
feat: add code to check, edit and restore shell scripts
1 parent 07fbe67 commit 2f340ad

2 files changed

Lines changed: 212 additions & 0 deletions

File tree

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
export function hasStartupCode(content: string, start: string, end: string, keys: string[]): boolean {
2+
const startIndex = content.indexOf(start);
3+
const endIndex = content.indexOf(end);
4+
if (startIndex === -1 || endIndex === -1 || startIndex >= endIndex) {
5+
return false;
6+
}
7+
const contentBetween = content.substring(startIndex + start.length, endIndex).trim();
8+
return contentBetween.length > 0 && keys.every((key) => contentBetween.includes(key));
9+
}
10+
11+
export function insertStartupCode(content: string, start: string, end: string, code: string): string {
12+
const startIndex = content.indexOf(start);
13+
const endIndex = content.indexOf(end);
14+
15+
if (startIndex !== -1 && endIndex !== -1 && startIndex < endIndex) {
16+
return content.substring(0, startIndex + start.length) + '\n\n' + code + '\n\n' + content.substring(endIndex);
17+
} else {
18+
return content + '\n' + start + '\n\n' + code + '\n\n' + end + '\n';
19+
}
20+
}
21+
22+
export function removeStartupCode(content: string, start: string, end: string): string {
23+
const startIndex = content.indexOf(start);
24+
const endIndex = content.indexOf(end);
25+
26+
if (startIndex !== -1 && endIndex !== -1 && startIndex < endIndex) {
27+
const before = content.substring(0, startIndex + start.length);
28+
const after = content.substring(endIndex);
29+
return before.trimEnd() + after;
30+
}
31+
return content;
32+
}
Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
import * as assert from 'assert';
2+
import {
3+
hasStartupCode,
4+
insertStartupCode,
5+
removeStartupCode,
6+
} from '../../../../../features/terminal/shells/common/editUtils';
7+
8+
suite('Shell Edit Utils', () => {
9+
suite('hasStartupCode', () => {
10+
test('should return false when no markers exist', () => {
11+
const content = 'sample content without markers';
12+
const result = hasStartupCode(content, '# START', '# END', ['key']);
13+
assert.strictEqual(result, false);
14+
});
15+
16+
test('should return false when only start marker exists', () => {
17+
const content = 'content\n# START\nsome code';
18+
const result = hasStartupCode(content, '# START', '# END', ['key']);
19+
assert.strictEqual(result, false);
20+
});
21+
22+
test('should return false when only end marker exists', () => {
23+
const content = 'content\nsome code\n# END';
24+
const result = hasStartupCode(content, '# START', '# END', ['key']);
25+
assert.strictEqual(result, false);
26+
});
27+
28+
test('should return false when markers are in wrong order', () => {
29+
const content = 'content\n# END\nsome code\n# START';
30+
const result = hasStartupCode(content, '# START', '# END', ['key']);
31+
assert.strictEqual(result, false);
32+
});
33+
34+
test('should return false when content between markers is empty', () => {
35+
const content = 'content\n# START\n# END\nmore content';
36+
const result = hasStartupCode(content, '# START', '# END', ['key']);
37+
assert.strictEqual(result, false);
38+
});
39+
40+
test('should return false when key is not found between markers', () => {
41+
const content = 'content\n# START\nsome other content\n# END\nmore content';
42+
const result = hasStartupCode(content, '# START', '# END', ['key']);
43+
assert.strictEqual(result, false);
44+
});
45+
46+
test('should return true when key is found between markers', () => {
47+
const content = 'content\n# START\nsome key content\n# END\nmore content';
48+
const result = hasStartupCode(content, '# START', '# END', ['key']);
49+
assert.strictEqual(result, true);
50+
});
51+
52+
test('should return true when all keys are found between markers', () => {
53+
const content = 'content\n# START\nsome key1 and key2 content\n# END\nmore content';
54+
const result = hasStartupCode(content, '# START', '# END', ['key1', 'key2']);
55+
assert.strictEqual(result, true);
56+
});
57+
58+
test('should return false when not all keys are found between markers', () => {
59+
const content = 'content\n# START\nsome key1 content\n# END\nmore content';
60+
const result = hasStartupCode(content, '# START', '# END', ['key1', 'key2']);
61+
assert.strictEqual(result, false);
62+
});
63+
});
64+
65+
suite('insertStartupCode', () => {
66+
test('should insert code at the end when no markers exist', () => {
67+
const content = 'existing content';
68+
const start = '# START';
69+
const end = '# END';
70+
const code = 'new code';
71+
72+
const result = insertStartupCode(content, start, end, code);
73+
const expected = 'existing content\n# START\nnew code\n# END\n';
74+
75+
assert.strictEqual(result, expected);
76+
});
77+
78+
test('should replace code between existing markers', () => {
79+
const content = 'before\n# START\nold code\n# END\nafter';
80+
const start = '# START';
81+
const end = '# END';
82+
const code = 'new code';
83+
84+
const result = insertStartupCode(content, start, end, code);
85+
const expected = 'before\n# START\nnew code\n# END\nafter';
86+
87+
assert.strictEqual(result, expected);
88+
});
89+
90+
test('should preserve content outside markers when replacing', () => {
91+
const content = 'line1\nline2\n# START\nold code\n# END\nline3\nline4';
92+
const start = '# START';
93+
const end = '# END';
94+
const code = 'new code';
95+
96+
const result = insertStartupCode(content, start, end, code);
97+
const expected = 'line1\nline2\n# START\nnew code\n# END\nline3\nline4';
98+
99+
assert.strictEqual(result, expected);
100+
});
101+
102+
test('should add new code when only start marker exists', () => {
103+
const content = 'before\n# START\nold code';
104+
const start = '# START';
105+
const end = '# END';
106+
const code = 'new code';
107+
108+
const result = insertStartupCode(content, start, end, code);
109+
const expected = 'before\n# START\nnew code\n# END\n';
110+
111+
assert.strictEqual(result, expected);
112+
});
113+
114+
test('should add new code when only end marker exists', () => {
115+
const content = 'before\nold code\n# END\nafter';
116+
const start = '# START';
117+
const end = '# END';
118+
const code = 'new code';
119+
120+
const result = insertStartupCode(content, start, end, code);
121+
const expected = 'before\nold code\n# END\nafter\n# START\nnew code\n# END\n';
122+
123+
assert.strictEqual(result, expected);
124+
});
125+
});
126+
127+
suite('removeStartupCode', () => {
128+
test('should return original content when no markers exist', () => {
129+
const content = 'sample content without markers';
130+
const result = removeStartupCode(content, '# START', '# END');
131+
assert.strictEqual(result, content);
132+
});
133+
134+
test('should return original content when only start marker exists', () => {
135+
const content = 'content\n# START\nsome code';
136+
const result = removeStartupCode(content, '# START', '# END');
137+
assert.strictEqual(result, content);
138+
});
139+
140+
test('should return original content when only end marker exists', () => {
141+
const content = 'content\nsome code\n# END';
142+
const result = removeStartupCode(content, '# START', '# END');
143+
assert.strictEqual(result, content);
144+
});
145+
146+
test('should return original content when markers are in wrong order', () => {
147+
const content = 'content\n# END\nsome code\n# START';
148+
const result = removeStartupCode(content, '# START', '# END');
149+
assert.strictEqual(result, content);
150+
});
151+
152+
test('should remove content between markers', () => {
153+
const content = 'before\n# START\ncode to remove\n# END\nafter';
154+
const result = removeStartupCode(content, '# START', '# END');
155+
const expected = 'before\nafter';
156+
assert.strictEqual(result, expected);
157+
});
158+
159+
test('should handle multiple lines of content between markers', () => {
160+
const content = 'line1\nline2\n# START\nline3\nline4\nline5\n# END\nline6\nline7';
161+
const result = removeStartupCode(content, '# START', '# END');
162+
const expected = 'line1\nline2\nline6\nline7';
163+
assert.strictEqual(result, expected);
164+
});
165+
166+
test('should handle markers at beginning of content', () => {
167+
const content = '# START\ncode to remove\n# END\nafter content';
168+
const result = removeStartupCode(content, '# START', '# END');
169+
const expected = 'after content';
170+
assert.strictEqual(result, expected);
171+
});
172+
173+
test('should handle markers at end of content', () => {
174+
const content = 'before content\n# START\ncode to remove\n# END';
175+
const result = removeStartupCode(content, '# START', '# END');
176+
const expected = 'before content';
177+
assert.strictEqual(result, expected);
178+
});
179+
});
180+
});

0 commit comments

Comments
 (0)