Commits

Anonymous committed c0d1efc

Added initial project creation tests. Though not hooked up to the webserver.

  • Participants
  • Parent commits 231589f
  • Branches erlang-backend

Comments (0)

Files changed (21)

File backend/erlang/templates/BespinSettings/commands/README.txt

+This is the directory for your commands.
+
+Name them: COMMAND_NAME.js, e.g. calculate.js.
+
+To access you can run:
+
+* cmdlist
+* cmdload commandname
+* cmdedit commandname
+* cmdadd commandname
+* cmdrm commandname

File backend/erlang/templates/BespinSettings/config

Empty file added.

File backend/erlang/templates/BespinSettings/plugins/README.txt

+------------------------------------------------------------------------------
+ Bespin Plugins Directory
+------------------------------------------------------------------------------
+
+This is where your Bespin plugins go.
+
+There are a few ways to install a new plugin:
+
+- Run "plugin install http://path/to/theme" and it will copy it in here
+- Manually create a file named YourPlugin.js that has the correct values in it (copy another theme to make sure)
+
+Now you can "plugin load YourPlugin" to see it in action.
+

File backend/erlang/templates/BespinSettings/scratchpad/README.txt

Empty file added.

File backend/erlang/templates/BespinSettings/themes/README.txt

+------------------------------------------------------------------------------
+ Bespin Themes Directory
+------------------------------------------------------------------------------
+
+This is where your custom themes go.
+
+There are a few ways to create a new theme:
+
+- Run "theme install http://path/to/theme" and it will copy it in
+- Manually create a file named YourTheme.js that has the correct values in it (copy another theme such as the "greenonblack.js" to make sure)
+
+Now you can "set theme YourTheme" to see it in action.
+

File backend/erlang/templates/BespinSettings/themes/greenonblack.js

+/* ***** BEGIN LICENSE BLOCK *****
+ * Version: MPL 1.1
+ *
+ * The contents of this file are subject to the Mozilla Public License
+ * Version 1.1 (the "License"); you may not use this file except in
+ * compliance with the License. You may obtain a copy of the License at
+ * http://www.mozilla.org/MPL/
+ *
+ * Software distributed under the License is distributed on an "AS IS"
+ * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
+ * See the License for the specific language governing rights and
+ * limitations under the License.
+ *
+ * The Original Code is Bespin.
+ *
+ * The Initial Developer of the Original Code is Mozilla.
+ * Portions created by the Initial Developer are Copyright (C) 2009
+ * the Initial Developer. All Rights Reserved.
+ *
+ * Contributor(s):
+ *   Bespin Team (bespin@mozilla.com)
+ *
+ * ***** END LICENSE BLOCK ***** */
+
+dojo.provide("bespin.themes.greenonblack");
+
+// = Green on Black Theme =
+bespin.themes.greenonblack = {
+    backgroundStyle: "#000000",
+    gutterStyle: "#d2d2d2",
+    lineNumberColor: "#888888",
+    lineNumberFont: "10pt Monaco, Lucida Console, monospace",
+    zebraStripeColor: "#000000", //"#111111",
+    highlightCurrentLineColor: "#3a312b",
+    editorTextFont: "10pt Monaco, Lucida Console, monospace",
+    editorTextColor: "#2fe41f",
+    editorSelectedTextColor: "rgb(240, 240, 240)",
+    editorSelectedTextBackground: "#243b75",
+    cursorStyle: "#879aff",
+    cursorType: "ibeam",       // one of "underline" or "ibeam"
+    unfocusedCursorStrokeStyle: "#FF0033",
+    unfocusedCursorFillStyle: "#73171E",
+    partialNibStyle: "rgba(100, 100, 100, 0.3)",
+    partialNibArrowStyle: "rgba(255, 255, 255, 0.3)",
+    partialNibStrokeStyle: "rgba(150, 150, 150, 0.3)",
+    fullNibStyle: "rgb(100, 100, 100)",
+    fullNibArrowStyle: "rgb(255, 255, 255)",
+    fullNibStrokeStyle: "rgb(150, 150, 150)",
+    scrollTrackFillStyle: "rgba(50, 50, 50, 0.8)",
+    scrollTrackStrokeStyle: "rgb(150, 150, 150)",
+    scrollBarFillStyle: "rgba(0, 0, 0, %a)",
+    scrollBarFillGradientTopStart: "rgba(90, 90, 90, %a)",
+    scrollBarFillGradientTopStop: "rgba(40, 40, 40, %a)",
+    scrollBarFillGradientBottomStart: "rgba(22, 22, 22, %a)",
+    scrollBarFillGradientBottomStop: "rgba(44, 44, 44, %a)",
+    tabSpace: "#E0D4CB",
+    searchHighlight: "#B55C00",
+    searchHighlightSelected: "#FF9A00",
+
+    // syntax definitions
+    plain: "#bdae9d",
+    preprocessor: "rgb(100,100,100)",
+    keyword: "#42a8ed",
+    string: "#039a0a",
+    comment: "#666666",
+    'c-comment': "#666666",
+    punctuation: "#888888",
+    attribute: "#BF9464",
+    test: "rgb(255,0,0)",
+    cdata: "#bdae9d",
+    "attribute-value": "#BF9464",
+    tag: "#bdae9d",
+    "tag-name": "#bdae9d",
+    value: "#BF9464",
+    important: "#990000",
+    cssclass: "#BF9464",
+    cssid: "#bdae9d"
+    
+    // Codemirror additions
+    // TODO:
+};
+
+// ** Black Zebra Theme **
+bespin.themes.greenonblackzebra = {};
+dojo.mixin(bespin.themes.greenonblackzebra, bespin.themes.greenonblack); 
+bespin.themes.greenonblackzebra.zebraStripeColor = '#111111';

File backend/erlang/templates/SampleProject/example.js

+/*
+ * This sample code
+ * is really lame
+ * but we're tired
+ */
+function welcome() {
+    return "to Bespin";
+}

File backend/erlang/templates/SampleProject/index.html

+<html>
+<head>
+    <title>Bespin Editor Sample</title>
+</head>
+<body>
+    <h1>Welcome to Bespin!</h1>
+    <p>You can edit and preview within this interface.</p>
+</body>
+</html>

File backend/erlang/templates/SampleProject/readme.txt

+Welcome to Bespin!
+------------------
+
+A few helpful tips:
+
+* To jump between the command line and the editor, simply hit Ctrl-J
+
+* To turn on "strictlines" mode, which means that you can't click anywhere in the editor, and instead are restricted to where content is, type: > set strictlines on
+
+Check out:
+
+* FAQ: https://wiki.mozilla.org/Labs/Bespin/FAQ
+* Our initial announcement: http://labs.mozilla.com/2009/02/introducing-bespin

File backend/erlang/user_service/src/bespin_user.erl

 %%%-------------------------------------------------------------------
 -module(bespin_user).
 
--export([new/4, authenticate/2, info/1]).
+-export([new/4, authenticate/2, info/1, create_user_dir/2]).
 
 -include("user_service/include/bespin_user.hrl").
+-include("user_service/include/config.hrl").
 
 %%--------------------------------------------------------------------
 %% @spec new(#bespin_config{}, string(), string(), string()) -> #user{}
 
 info(User) ->
   {userinfo, User#user.username, 927, User#user.quota * 1000}.
+
+%% Creates the initial user directory and populates it 
+%% with the default projects.
+create_user_dir(User, Config) ->
+  Dir = filename:join(Config#bespin_config.devfiles_dir, User#user.username),
+  case filelib:is_dir(Dir) of
+    true ->
+      ok;
+    false ->
+      filelib:ensure_dir(Dir),
+      file:make_dir(Dir),
+      template:copy(Config#bespin_config.template_dir, "BespinSettings", Dir),
+      template:copy(Config#bespin_config.template_dir, "SampleProject", Dir)
+  end.

File backend/erlang/user_service/src/bespin_user_service.erl

 -behaviour(gen_server).
 
 -include("user_service/include/bespin_user.hrl").
+-include("user_service/include/config.hrl").
 
 %% API
 -export([start_link/0,stop/0]).

File backend/erlang/user_service/src/template.erl

+-module(template).
+
+-include_lib("kernel/include/file.hrl").
+
+-include("user_service/include/bespin_user.hrl").
+
+-export([copy/3]).
+
+dir(Src, Target_base_dir) ->
+  Output_dir = filename:join(Target_base_dir, Src),
+  filelib:ensure_dir(Output_dir),
+  file:make_dir(Output_dir),
+  ok.
+
+file(Template_dir, Src, Target_base_dir) ->
+  Input_file = filename:join(Template_dir, Src),
+  Output_file = filename:join(Target_base_dir, Src),
+  {ok, Text} = file:read_file(Input_file),
+  file:write_file(Output_file, Text),
+  ok.
+
+%% Copies a file or folder recursively
+%% to the output folder.
+copy(Template_dir, Src, Target_base_dir) ->
+  Current = filename:join(Template_dir, Src),
+  case file:read_file_info(Current) of
+    {ok, #file_info{type=directory}} ->
+      dir(Src, Target_base_dir),
+      {ok, Files} = file:list_dir(Current),
+      lists:foreach(
+        fun(File) ->
+            copy(Template_dir, filename:join(Src, File), Target_base_dir)
+        end,
+        Files);
+    {ok, #file_info{type=regular}} ->
+      file(Template_dir, Src, Target_base_dir);
+    {ok, _} ->
+      ok
+  end.

File backend/erlang/user_service/tests/all_tests.erl

 run() ->
   eunit:test(bespin_user_tests),
   eunit:test(user_repo_tests),
+  eunit:test(template_tests),
   eunit:test(bespin_user_service_tests).

File backend/erlang/user_service/tests/bespin_user_tests.erl

 
 -include("user_service/include/bespin_user.hrl").
 
+-define(devfiles_folder, "tmp/devfiles").
+
 %% User Creation Tests ------------------------------------------
 
 create_user_test_() ->
 should_not_authenticate_user_if_password_does_not_match_test() ->
   User = test_helper:get_example_user(),
   ?assertEqual({error, bad_password}, bespin_user:authenticate(User, "badpass")).
+
+%% Create user directory tests
+
+user_directory_test_() ->
+  {setup, fun test_helper:create_devfiles_dir/0,
+          fun test_helper:teardown_devfiles_dir/1, 
+          [fun should_create_folder_in_dev_files_for_new_user/0,
+           fun should_copy_in_settings_project_template/0,
+           fun should_copy_example_project_template/0]}.
+
+should_create_folder_in_dev_files_for_new_user() ->
+  bespin_user:create_user_dir(test_helper:get_example_user(), test_helper:get_config()),
+  ?_assert(filelib:is_dir(?devfiles_folder++"/kanej")).
+
+should_copy_in_settings_project_template() ->
+  bespin_user:create_user_dir(test_helper:get_example_user(), test_helper:get_config()),
+  ?assert(filelib:is_dir(?devfiles_folder++"/kanej/BespinSettings")).
+
+should_copy_example_project_template() ->
+  bespin_user:create_user_dir(test_helper:get_example_user(), test_helper:get_config()),
+  ?assert(filelib:is_dir(?devfiles_folder++"/kanej/SampleProject")).
+

File backend/erlang/user_service/tests/content/templates/a_file.txt

+some text.

File backend/erlang/user_service/tests/content/templates/files/a_file.txt

+some text.

File backend/erlang/user_service/tests/content/templates/files/another_file.txt

+some more text.

File backend/erlang/user_service/tests/content/templates/subfolder/sub1/a_file.txt

+some text.

File backend/erlang/user_service/tests/content/templates/subfolder/sub2/another_file.txt

+some text.

File backend/erlang/user_service/tests/template_tests.erl

+
+-module(template_tests).
+
+-include_lib("eunit/include/eunit.hrl").
+
+-include("user_service/include/bespin_user.hrl").
+
+-define(Tmp_dir, "tmp").
+-define(Template_dir, "user_service/tests/content/templates").
+
+%% Create Template Tests
+
+create_template_dir_test_() ->
+  {setup, fun test_helper:create_tmp_dir/0,
+          fun test_helper:teardown_tmp_dir/1, 
+          [fun should_copy_empty_folder/0,
+           fun should_copy_file/0,
+           fun should_copy_folder_and_its_files/0,
+           fun should_copy_subfolders_and_their_files/0]}.
+
+should_copy_empty_folder() ->
+  template:copy(?Template_dir, "empty", ?Tmp_dir),
+  ?assert(filelib:is_dir(?Tmp_dir++"/empty")).
+
+should_copy_file() ->
+  template:copy(?Template_dir, "a_file.txt", ?Tmp_dir),
+  ?assert(filelib:is_regular(?Tmp_dir++"/a_file.txt")).
+
+should_copy_folder_and_its_files() ->
+  template:copy(?Template_dir, "files", ?Tmp_dir),
+  ?assert(filelib:is_dir(?Tmp_dir++"/files")),
+  ?assert(filelib:is_regular(?Tmp_dir++"/files/a_file.txt")),
+  ?assert(filelib:is_regular(?Tmp_dir++"/files/another_file.txt")).
+
+should_copy_subfolders_and_their_files() ->
+  template:copy(?Template_dir, "subfolder", ?Tmp_dir),
+  ?assert(filelib:is_dir(?Tmp_dir++"/subfolder")),
+  ?assert(filelib:is_regular(?Tmp_dir++"/subfolder/sub1/a_file.txt")),
+  ?assert(filelib:is_regular(?Tmp_dir++"/subfolder/sub2/another_file.txt")).

File backend/erlang/user_service/tests/test_helper.erl

 -module(test_helper).
 
--export([get_example_user/0]).
+-export([create_tmp_dir/0,
+         teardown_tmp_dir/1,
+         create_devfiles_dir/0,
+         teardown_devfiles_dir/1,
+         get_config/0,
+         get_example_user/0]).
+
 
 -include_lib("kernel/include/file.hrl").
 
 -include("user_service/include/bespin_user.hrl").
+-include("user_service/include/config.hrl").
+
+
+-define(devfiles_dir, "tmp/devfiles").
+-define(Tmp_dir, "tmp").
+-define(Template_dir, "user_service/tests/content/templates").
+
 
 %% Helper Functions
 
 get_example_user() ->
   bespin_user:new(#bespin_config{}, "kanej", "kanej@example.com", "pass1").
+
+get_config() ->
+  #bespin_config{devfiles_dir = ?devfiles_dir}.
+
+create_tmp_dir() ->
+  filelib:ensure_dir(?Tmp_dir),
+  file:make_dir(?Tmp_dir).
+
+create_devfiles_dir() ->
+  filelib:ensure_dir(?devfiles_dir),
+  file:make_dir(?devfiles_dir).
+
+teardown_tmp_dir(_) ->
+  delete(?Tmp_dir).
+
+teardown_devfiles_dir(_) ->
+  delete(?devfiles_dir).
+
+delete(Src) ->
+  case file:read_file_info(Src) of
+    {ok, #file_info{type=directory}} ->
+      {ok, Files} = file:list_dir(Src),
+      lists:foreach(
+        fun(File) ->
+            delete(filename:join(Src, File))
+        end,
+        Files),
+      file:del_dir(Src);
+    {ok, #file_info{type=regular}} ->
+      file:delete(Src);
+    {ok, _} ->
+      ok
+  end.