Commit f7fbd9ab authored by Natalia Tepluhina's avatar Natalia Tepluhina

Merge branch '33460-webide-line-endings' into 'master'

Resolve "Web IDE does not create POSIX Compliant Files"

See merge request gitlab-org/gitlab!19339
parents 4c68d463 9fa41499
......@@ -5,7 +5,7 @@ import eventHub from '../../eventhub';
import service from '../../services';
import * as types from '../mutation_types';
import router from '../../ide_router';
import { setPageTitle, replaceFileUrl } from '../utils';
import { setPageTitle, replaceFileUrl, addFinalNewlineIfNeeded } from '../utils';
import { viewerTypes, stageKeys } from '../../constants';
export const closeFile = ({ commit, state, dispatch }, file) => {
......@@ -140,7 +140,10 @@ export const getRawFileData = ({ state, commit, dispatch, getters }, { path }) =
export const changeFileContent = ({ commit, dispatch, state }, { path, content }) => {
const file = state.entries[path];
commit(types.UPDATE_FILE_CONTENT, { path, content });
commit(types.UPDATE_FILE_CONTENT, {
path,
content: addFinalNewlineIfNeeded(content),
});
const indexOfChangedFile = state.changedFiles.findIndex(f => f.path === path);
......
......@@ -269,3 +269,7 @@ export const pathsAreEqual = (a, b) => {
return cleanA === cleanB;
};
// if the contents of a file dont end with a newline, this function adds a newline
export const addFinalNewlineIfNeeded = content =>
content.charAt(content.length - 1) !== '\n' ? `${content}\n` : content;
---
title: 'Resolve: Web IDE does not create POSIX Compliant Files'
merge_request: 19339
author:
type: fixed
......@@ -261,10 +261,10 @@ describe('RepoEditor', () => {
});
it('updates state when model content changed', done => {
vm.model.setValue('testing 123');
vm.model.setValue('testing 123\n');
setTimeout(() => {
expect(vm.file.content).toBe('testing 123');
expect(vm.file.content).toBe('testing 123\n');
done();
});
......
......@@ -455,17 +455,33 @@ describe('IDE store file actions', () => {
beforeEach(() => {
tmpFile = file('tmpFile');
tmpFile.content = '\n';
tmpFile.raw = '\n';
store.state.entries[tmpFile.path] = tmpFile;
});
it('updates file content', done => {
store
.dispatch('changeFileContent', {
path: tmpFile.path,
content: 'content\n',
})
.then(() => {
expect(tmpFile.content).toBe('content\n');
done();
})
.catch(done.fail);
});
it('adds a newline to the end of the file if it doesnt already exist', done => {
store
.dispatch('changeFileContent', {
path: tmpFile.path,
content: 'content',
})
.then(() => {
expect(tmpFile.content).toBe('content');
expect(tmpFile.content).toBe('content\n');
done();
})
......@@ -510,12 +526,12 @@ describe('IDE store file actions', () => {
store
.dispatch('changeFileContent', {
path: tmpFile.path,
content: 'content',
content: 'content\n',
})
.then(() =>
store.dispatch('changeFileContent', {
path: tmpFile.path,
content: '',
content: '\n',
}),
)
.then(() => {
......
......@@ -292,6 +292,8 @@ describe('IDE commit module actions', () => {
type: 'blob',
active: true,
lastCommitSha: TEST_COMMIT_SHA,
content: '\n',
raw: '\n',
};
Object.assign(store.state, {
......@@ -359,7 +361,7 @@ describe('IDE commit module actions', () => {
{
action: commitActionTypes.update,
file_path: jasmine.anything(),
content: undefined,
content: '\n',
encoding: jasmine.anything(),
last_commit_id: undefined,
previous_path: undefined,
......@@ -386,7 +388,7 @@ describe('IDE commit module actions', () => {
{
action: commitActionTypes.update,
file_path: jasmine.anything(),
content: undefined,
content: '\n',
encoding: jasmine.anything(),
last_commit_id: TEST_COMMIT_SHA,
previous_path: undefined,
......
......@@ -597,4 +597,17 @@ describe('Multi-file store utils', () => {
});
});
});
describe('addFinalNewlineIfNeeded', () => {
it('adds a newline if it doesnt already exist', () => {
[
{ input: 'some text', output: 'some text\n' },
{ input: 'some text\n', output: 'some text\n' },
{ input: 'some text\n\n', output: 'some text\n\n' },
{ input: 'some\n text', output: 'some\n text\n' },
].forEach(({ input, output }) => {
expect(utils.addFinalNewlineIfNeeded(input)).toEqual(output);
});
});
});
});
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment